Skip to content

Instantly share code, notes, and snippets.

@Plus1XP
Created April 13, 2019 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plus1XP/13788f9fcf3bdf9c35f9e9d7936b4b62 to your computer and use it in GitHub Desktop.
Save Plus1XP/13788f9fcf3bdf9c35f9e9d7936b4b62 to your computer and use it in GitHub Desktop.
Iterates through a string replacing characters with a character of your choice
public string ReplaceLetters(string wordToSearch, char letterToFind, char letterToReplace)
{
StringBuilder word = new StringBuilder(wordToSearch);
int index = 0;
foreach (char letter in wordToSearch)
{
if (letter.Equals(letterToFind))
{
word.Remove(index, 1);
word.Insert(index, letterToReplace);
}
index++;
}
return word;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment