Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. public static class StringExtensions
  2. {
  3. public static string SafeReplace(this string input, string find, string replace, bool matchWholeWord)
  4. {
  5. string textToFind = matchWholeWord ? string.Format(@"b{0}b", find) : find;
  6. return Regex.Replace(input, textToFind, replace);
  7. }
  8. }
  9.  
  10. string text = "Add Additional String to text box";
  11. string result = text.SafeReplace("Add", "Insert", true);
  12.  
  13. string pattern = @"bAddb";
  14. string input = "Add Additional String to text box";
  15. string result = Regex.Replace(input, pattern, "Insert", RegexOptions.None);
  16.  
  17. string input = "Add Additional String to text box";
  18. string output = input.replace("Add ", "Insert ");
  19.  
  20. output == "Insert Additional String to text box" // true
  21.  
  22. // add a leading and tail space
  23. string tmp = " " + "Add Additional String to text box"+ " ";
  24. // replace the word you want, while adding a lead and tail space, and then Trim
  25. tmp = tmp.Replace(" Add ", " Insert ").Trim();
  26.  
  27. //Find and Replace A word in c#
  28. public static class Program
  29. {
  30. public static string Replace(this String str, char[] chars, string replacement)
  31. {
  32. StringBuilder output = new StringBuilder(str.Length);
  33. bool replace = false;
  34. if (chars.Length - 1 < 1)
  35. {
  36. for (int i = 0; i < str.Length; i++)
  37. {
  38.  
  39. char c = str[i];
  40. replace = false;
  41. // int val = Regex.Matches(ch.ToString(), @"[a-zA-Z]").Count;
  42.  
  43.  
  44. for (int j = 0; j < chars.Length; j++)
  45. {
  46. if (chars[j] == c)
  47. {
  48. replace = true;
  49.  
  50. break;
  51.  
  52. }
  53.  
  54. }
  55.  
  56. if (replace)
  57. output.Append(replacement);
  58. else
  59. output.Append(c);
  60. }
  61. }
  62. else
  63. {
  64.  
  65. int j = 0;
  66. int truecount = 0;
  67. char c1 = '';
  68. for (int k = 0; k < str.Length; k++)
  69. {
  70. c1 = str[k];
  71.  
  72. if (chars[j] == c1)
  73. {
  74. replace = true;
  75. truecount ++;
  76.  
  77.  
  78. }
  79.  
  80. else
  81. {
  82. truecount = 0;
  83. replace = false;
  84. j = 0;
  85. }
  86. if(truecount>0)
  87. {
  88. j++;
  89. }
  90.  
  91. if (j > chars.Length-1)
  92. {
  93. j = 0;
  94. }
  95.  
  96. if (truecount == chars.Length)
  97. {
  98. output.Remove(output.Length - chars.Length+1, chars.Length-1);
  99. // output.Remove(4, 2);
  100. if (replace)
  101. output.Append(replacement);
  102. else
  103. output.Append(c1);
  104. }
  105. else
  106. {
  107. output.Append(c1);
  108. }
  109.  
  110.  
  111. }
  112.  
  113. }
  114.  
  115.  
  116. return output.ToString();
  117. }
  118.  
  119.  
  120. static void Main(string[] args)
  121. {
  122.  
  123. Console.WriteLine("Enter a word");
  124. string word = (Console.ReadLine());
  125.  
  126. Console.WriteLine("Enter a word to find");
  127. string find = (Console.ReadLine());
  128.  
  129. Console.WriteLine("Enter a word to Replace");
  130. string Rep = (Console.ReadLine());
  131.  
  132. Console.WriteLine(Replace(word, find.ToCharArray(), Rep));
  133. Console.ReadLine();
  134.  
  135. }
  136.  
  137. }
  138.  
  139. public static string SafeReplace(this string input, string find, string replace, bool matchWholeWord) {
  140. string searchString = find.StartsWith("@") ? $@"@b{find.Substring(1)}b" : $@"b{find}b";
  141. string textToFind = matchWholeWord ? searchString : find;
  142. return Regex.Replace(input, textToFind, replace);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement