By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

replacing a char in a string

P: n/a
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.
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Nov 17 '05 #2

P: n/a
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
"VMI" <vo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Nov 17 '05 #3

P: n/a
KH
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.


Nov 17 '05 #4

P: n/a
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

"VMI" <vo******@yahoo.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Nov 17 '05 #5

P: n/a
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[i])
{
case 'á':
{
cIadl2[i] = 'A';
break;
}
case 'é':
{
cIadl2[i] = 'E';
break;
}
case 'í':
{
cIadl2[i] = 'I';
break;
}
case 'Ñ':
{
cIadl2[i] = 'N';
break;
}
// Plus 10 more of these for a total of 15
}
}


"VMI" <vo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Nov 17 '05 #6

P: n/a
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
"VMI" <vo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
| 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[i])
| {
| case 'á':
| {
| cIadl2[i] = 'A';
| break;
| }
| case 'é':
| {
| cIadl2[i] = 'E';
| break;
| }
| case 'í':
| {
| cIadl2[i] = 'I';
| break;
| }
| case 'Ñ':
| {
| cIadl2[i] = 'N';
| break;
| }
| // Plus 10 more of these for a total of 15
| }
| }
|
|
|
|
| "VMI" <vo******@yahoo.com> wrote in message
| news:%2****************@TK2MSFTNGP12.phx.gbl...
| > 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.
| >
|
|
Nov 17 '05 #7

P: n/a
Octavio Hernandez wrote:
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

"VMI" <vo******@yahoo.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.