473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Replacing a Character in a String

What's the "standard" way of replacing a character in a string?
Obviously, I can't say "myString[index] = character;" because strings
are immutable...

So what would be the "standard" way of doing this?

TIA
Nov 15 '05 #1
4 22684
C# Learner wrote:
Obviously, I can't say "myString[index] = character;" because strings
are immutable...


OK, if the above is a no-no for you, then use the StringBuilder class.

--
gabriel
Nov 15 '05 #2
myString = myString.Replace("xyz", "abc")

Tu-Thac

----- C# Learner wrote: ----

What's the "standard" way of replacing a character in a string
Obviously, I can't say "myString[index] = character;" because string
are immutable..

So what would be the "standard" way of doing this

TI

Nov 15 '05 #3
Dont know if there is necessarily a "standard" way, but here
is what I would do:

StringBuilder s = new StringBuilder("astringoftext");
s[11] = 'q';
string sNew = s.ToString();

sNew is now "astringofteqt"

and can now be assigned to the original string..

"C# Learner" <cs****@learner.here> wrote in message
news:f6********************************@4ax.com...
What's the "standard" way of replacing a character in a string?
Obviously, I can't say "myString[index] = character;" because strings
are immutable...

So what would be the "standard" way of doing this?

TIA

Nov 15 '05 #4
What's the "standard" way of replacing a character in a string?
Obviously, I can't say "myString[index] = character;" because strings
are immutable...

So what would be the "standard" way of doing this?


If you want to change one character with another one
use String.Replace

If you want to change one char at a certain index
use String.ToCharArray, do your change, then construct a new string from it
(not the most efficient way)

If it is necessary to modify the actual contents of a string-like object, use
the System.Text.StringBuilder class.
--
Mihai
-------------------------
Replace _year_ with _ to get the real email
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ren? M?hle | last post by:
I have a psp script with a procedure just to run an update on one table. The Problem occurs when I try to compile this script with pspload: ORA-20006: "frsys_updatereport.psp": compilation...
1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
2
by: jt | last post by:
Looking for an example how to convert and CString to an ASCII character string. Any examples on how to do this? Thank you, jt
0
by: MLH | last post by:
Is an apostrophe a character of special significance to MySQL in a way that would cause "Bob's dog" to become translated into a 12-character string when typed into a MySQL memo field? If I type...
7
by: Justin | last post by:
i need to build the unsigned character string: "PR0N\0Spam\0G1RLS\0Other\0Items\0\0\0" from the signed character string: "PR0N Spam G1RLS Other Items" Tokeninzing the character string is not...
5
by: Karthik | last post by:
Hello! I am not a wizard in this area! Just need some help out in this. I am trying to convert bstr string to new character string. Here is the snippet of my code. **** Code Start**** ...
25
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library...
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
0
by: Tor Inge Rislaa | last post by:
Replacing character with ASCII code (HTML) Is it possible to replace a given character or symbol with it's ASCII code within a <ptag in HTML code. TIRislaa
3
by: Gmuchan | last post by:
Hi, Help needed please. Basically I am wanting to update a 16 digit character string which is in a table called say accounts. If the account no. field is say 0502562389568745 I want to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.