L
Lonifasiko
Hi,
Just want to replace character at index 1 of a string with another
character. Just want to replace character at that position. I thought
Replace method would be overloaded with an index parameter with which
you can write wanted character at that position. But no, Replace
method only allows replacing one known character with another. The
problem is I don't know the character to replace, just must replace
the character at a known index.
This is the first workaround I've found:
string s = "ihj";
s = s.Remove(1, 1).Insert(1, "p");
And here is the second one:
string s = "ihj";
char[] array = s.ToCharArray();
array[1] = 'p';
s = new string(array);
A bit complicated,, don't you think? I think String.Replace should be
overloaded with something like this:
string Replace (int index, char newCharacter);
Any comments?
Thanks in advance.
Just want to replace character at index 1 of a string with another
character. Just want to replace character at that position. I thought
Replace method would be overloaded with an index parameter with which
you can write wanted character at that position. But no, Replace
method only allows replacing one known character with another. The
problem is I don't know the character to replace, just must replace
the character at a known index.
This is the first workaround I've found:
string s = "ihj";
s = s.Remove(1, 1).Insert(1, "p");
And here is the second one:
string s = "ihj";
char[] array = s.ToCharArray();
array[1] = 'p';
s = new string(array);
A bit complicated,, don't you think? I think String.Replace should be
overloaded with something like this:
string Replace (int index, char newCharacter);
Any comments?
Thanks in advance.