c# replace string code example
Example 1: c sharp string replace
str = "Hello World!"
str.Replace('o', 'e');
Example 2: String.Replace() c#
String s = "aaa";
Console.WriteLine($"The initial string: {s}");
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine($"Final String: {s}");
Example 3: c# Replace Strings In C#
string odd = "1, 3, 5, 7, 9, 11, 13, 15, 17, 19";
Console.WriteLine($ "Original odd: {odd}");
string newOdd = odd.Replace(',', ':');
Console.WriteLine($ "New Odd: {newOdd}");
string authors = "Mahesh Beniwal, Neel Beniwal, Raj Beniwal, Dinesh Beniwal";
Console.WriteLine($ "Authors with last names: {authors}");
string firstNames = authors.Replace(" Beniwal", "");
Console.WriteLine($ "Authors without last names: {firstNames}");
Example 4: modificare una strinfa in c#
string phrase = "The quick brown fox jumps over the fence";
Console.WriteLine(phrase);
char[] phraseAsChars = phrase.ToCharArray();
int animalIndex = phrase.IndexOf("fox");
if (animalIndex != -1)
{
phraseAsChars[animalIndex++] = 'c';
phraseAsChars[animalIndex++] = 'a';
phraseAsChars[animalIndex] = 't';
}
string updatedPhrase = new string(phraseAsChars);
Console.WriteLine(updatedPhrase);