Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class StringExtensions
- {
- public static string SafeReplace(this string input, string find, string replace, bool matchWholeWord)
- {
- string textToFind = matchWholeWord ? string.Format(@"b{0}b", find) : find;
- return Regex.Replace(input, textToFind, replace);
- }
- }
- string text = "Add Additional String to text box";
- string result = text.SafeReplace("Add", "Insert", true);
- string pattern = @"bAddb";
- string input = "Add Additional String to text box";
- string result = Regex.Replace(input, pattern, "Insert", RegexOptions.None);
- string input = "Add Additional String to text box";
- string output = input.replace("Add ", "Insert ");
- output == "Insert Additional String to text box" // true
- // add a leading and tail space
- string tmp = " " + "Add Additional String to text box"+ " ";
- // replace the word you want, while adding a lead and tail space, and then Trim
- tmp = tmp.Replace(" Add ", " Insert ").Trim();
- //Find and Replace A word in c#
- public static class Program
- {
- public static string Replace(this String str, char[] chars, string replacement)
- {
- StringBuilder output = new StringBuilder(str.Length);
- bool replace = false;
- if (chars.Length - 1 < 1)
- {
- for (int i = 0; i < str.Length; i++)
- {
- char c = str[i];
- replace = false;
- // int val = Regex.Matches(ch.ToString(), @"[a-zA-Z]").Count;
- for (int j = 0; j < chars.Length; j++)
- {
- if (chars[j] == c)
- {
- replace = true;
- break;
- }
- }
- if (replace)
- output.Append(replacement);
- else
- output.Append(c);
- }
- }
- else
- {
- int j = 0;
- int truecount = 0;
- char c1 = '