-1

I used replace keyword to replace string or char, and i can replace successfully here is my code

string keywoord = "Emerika";
var result = keywoord.Replace(keywoord[0], 'A');

the result : Amerika

but i get strange behaviour when replace thing and the next thing is same

 string keywoord = "xxxxx";
    var result = keywoord.Replace(keywoord[0], 'A');

I will get result : AAAAA

It should return Axxxx

string keywoord = "aaakaaa";
      var result = keywoord.Replace(keywoord[0], 'z');

I will get result : zzzkzzz

It should return zaakaaa

so what happened with this, i just need to replace one thing not multiple thing, any error or do u have another solution?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    Why do you expect that `Replace('x', 'A');` will replace only one occurrence? – Progman Nov 03 '19 at 18:01
  • keyword[0] = a So you're replacing all a with z – Mikael Nov 03 '19 at 18:01
  • no keyword[0] mean that i replace only first – Nur Mahin Nov 03 '19 at 18:03
  • @Progman i think it occure one because it's not looping – Nur Mahin Nov 03 '19 at 18:04
  • @NurMahin, The first parameter you're passing to `Replace` is the character you want to search and replace, not the position in the string that you want to replace. – Nimrod Dolev Nov 03 '19 at 18:05
  • @Mikael i just one replace first index, why replace acts as loop – Nur Mahin Nov 03 '19 at 18:05
  • @NurMahin I'm not sure why you're arguing, look up how .Replace works. https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8 You're basically saying -- Replace all occurrences of keyword[0] (which is a) with z. It's performing as intended. – Mikael Nov 03 '19 at 18:05
  • @NimrodDolev is it ? i have just known, how can solve it ? – Nur Mahin Nov 03 '19 at 18:06
  • The definition of [Replace](https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8) is: Returns a new string in which _all_ occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character. – devNull Nov 03 '19 at 18:07
  • yes you are right , how to specify the position @Mikael – Nur Mahin Nov 03 '19 at 18:07
  • i think it not answer and duplicate i want to replace something with another based on positon also, how ? – Nur Mahin Nov 03 '19 at 18:09
  • @devNull how can i use replace is it possible ? – Nur Mahin Nov 03 '19 at 18:09
  • Is your intention to just replace the first character in the string (whatever it is) by another character? Then e.g. `var sb = new StringBuilder(keywoord); sb[0] = 'A'; result = sb.ToString();` would do the trick. – Klaus Gütter Nov 03 '19 at 20:12

2 Answers2

0

If you want to replace the first n apparitions you can use Regex.

var regex = new Regex(Regex.Escape("o"));
var newText = regex.Replace("Hello World", "Foo", 1);

//result HellFoo World

  • "o" is the text witch you want to replace with
  • "Hello World" is the text in which you want to make changes
  • "Foo" is the replacement
  • 1(the third parameter) is the number of replacements(the first n occurrences to replace)
Marius Vuscan
  • 170
  • 1
  • 2
  • 16
0

A couple of things to understand -

  1. Some Replace implementations, for example in JavaScript, only replace the first occurrence of the searched string. The C# implementations do not, it replaces ANY occurrence, so if you replace every "x" in "xxxx" with an "A", you get "AAAA".

  2. The first parameter you pass to Replace is the value that you want to look for, and the second parameter is the value you want to replace it with.

See these examples -

"Emerika".Replace('E', 'A'); // "America"
"Emerika".Replace('k', 'A'); // "EmeriAa"
"Emerika".Replace('x', 'A'); // "Emerika", because there is no 'x' to replace
"xxxx".Replace('g', 'a'); // "xxxx", becuase there is no 'g' to replace
"xxxx".Replace('x', 'A'); // "AAAA", becuase every 'x' was replaced with an 'A'

var str = "Emerika";
str.Replace(str[0], 'A'); // "America", but just because the value of str[0] is equal to 'E', so this is just like out first example.

Here's a link to the MSDN page for String.Replace with some more explanations

Nimrod Dolev
  • 577
  • 3
  • 8