replacing a char in a string

V

VMI

If I have the string "Héllo", how can I replace char[1] (é) with an 'e'? I
cannot use the String.Replace() fuction. It has to be by replacing one char
with another.

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

VMI,

It's a little tedious, but why not just use the Substring method to
return the appropriate sections of the string. You can find the index of
the character, then replace it? Then, you can concatenate all of the
sections together.

For example.

// The old string.
string oldString = "Héllo";

// The new string.
string newString = oldString.Substring(0, 1) + "e" + oldString.Substring(2);

Hope this helps.
 
R

Raj Chudasama

it has an overload method that works for the characters too.
so string.replace works for string with length of more than 1 character or 1
character
u can also use string.replace for only one character by using the overload

ex:

strData = strData.Replace("\r","");

strData = strData.Replace('a','b');





hth
 
G

Guest

If you're going to be doing a lot of replacements like that you may want to
use a StringBuilder instead of strings.



Nicholas Paldino said:
VMI,

It's a little tedious, but why not just use the Substring method to
return the appropriate sections of the string. You can find the index of
the character, then replace it? Then, you can concatenate all of the
sections together.

For example.

// The old string.
string oldString = "Héllo";

// The new string.
string newString = oldString.Substring(0, 1) + "e" + oldString.Substring(2);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

VMI said:
If I have the string "Héllo", how can I replace char[1] (é) with an 'e'?
I cannot use the String.Replace() fuction. It has to be by replacing one
char with another.

Thanks.
 
O

Octavio Hernandez

VMI,

In C# (as in Java) strings are immutable. That is, you can't change its
value once it is assigned. All string operators and methods (like Replace)
work by producing new string instances instead of "writing over" their
operands.

Take a look at the System.Text.StringBuilder class if you need in-place
access to the characters that compose a string.

Regards - Octavio
 
V

VMI

Thanks for the info.
I was told that if I wanted to do several char replacements, it'd be faster
if I do it with a Switch...case with each char like the following rather
than doing a String.Replace() on the same string 15 times. I'll be doing
this process with 200,000 strings.

for (int i = 0; i < cIadl2.Length; i++)
{
switch (cIadl2)
{
case 'á':
{
cIadl2 = 'A';
break;
}
case 'é':
{
cIadl2 = 'E';
break;
}
case 'í':
{
cIadl2 = 'I';
break;
}
case 'Ñ':
{
cIadl2 = 'N';
break;
}
// Plus 10 more of these for a total of 15
}
}
 
J

Jay B. Harlow [MVP - Outlook]

VMI,
I agree the switch case will probably be faster then String.Replace.

However I'm not sure if the switch case will be "better" then
StringBuilder.Replace, although the switch case might still be faster.

StringBuilder sb = new StringBuilder(inputString);

sb.Replace('á', 'A');
sb.Replace('é', 'E');
sb.Replace('í', 'I');
sb.Replace('Ñ', 'N');

If you store the character replacement pairs in some form of data structure
the StringBuilder approach becomes even better looking.

The "problem" with the switch case, is that you need to evaluate the entire
switch for each character replacement pair, while the "problem" with
StringBuilder.Replace is you need to evaluate the entire string for each
character replacement.

In this case I would consider using RegEx.Replace, where the regular
expression are the 15 characters you want replaced, and the MatchEvaluator
returns what character to replace it with.

Also, it appears that you are doing some standard "encoding", have you
looked at the System.Text.Encoding class to see if there are a couple of
Encodings that you are mapping characters to & from?

Of course profiling & timing the 5 methods would identify which might be
best for your application:
- String.Replace
- StringBuilder.Replace
- RegEx.Replace w/MatchEvaluator
- your existing switch case
- System.Text.Encoding

Hope this helps
Jay


| Thanks for the info.
| I was told that if I wanted to do several char replacements, it'd be
faster
| if I do it with a Switch...case with each char like the following rather
| than doing a String.Replace() on the same string 15 times. I'll be doing
| this process with 200,000 strings.
|
| for (int i = 0; i < cIadl2.Length; i++)
| {
| switch (cIadl2)
| {
| case 'á':
| {
| cIadl2 = 'A';
| break;
| }
| case 'é':
| {
| cIadl2 = 'E';
| break;
| }
| case 'í':
| {
| cIadl2 = 'I';
| break;
| }
| case 'Ñ':
| {
| cIadl2 = 'N';
| break;
| }
| // Plus 10 more of these for a total of 15
| }
| }
|
|
|
|
| | > If I have the string "Héllo", how can I replace char[1] (é) with an 'e'?
| > I cannot use the String.Replace() fuction. It has to be by replacing
one
| > char with another.
| >
| > Thanks.
| >
|
|
 
M

MacKenzieMouse

Octavio said:
VMI,

In C# (as in Java) strings are immutable. That is, you can't change its
value once it is assigned. All string operators and methods (like Replace)
work by producing new string instances instead of "writing over" their
operands.

Take a look at the System.Text.StringBuilder class if you need in-place
access to the characters that compose a string.

Regards - Octavio

If I have the string "Héllo", how can I replace char[1] (é) with an 'e'?
I cannot use the String.Replace() fuction. It has to be by replacing one
char with another.

Thanks.
you can reuse the string like so:

string myString;
myString = "hello There";
myString = myString.Replace("T","t");
//myString now = "hello there"

I'm not in visual studio, so I can't test the syntax, but I have done
this successfully.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top