C # Replace a custom string,

Source: Internet
Author: User
Tags string find

C # Replace a custom string,

Some parts of the original string are replaced with the specified new string segment, for example, the cde in the source string: abcdeabcdfbcdefg is replaced with 12345, the resulting string is ab12345abcdfb12345fg, that is, abcdeabcdfbcdefg-> ab12345abcdfb12345fg.

Obviously, string. replace method. You need to customize a method string Replace (string originalString, string strToBeReplaced, string strToReplace). The following is my implementation code, which will be completed within half an hour, after debugging and testing and verifying the general data, you can still pass the test.

 1 public static string Replace(string originalString, string strToBeReplaced, string strToReplace) 2         { 3             string resultString = null; 4             char[] originalCharArray = originalString.ToCharArray(); 5             char[] strToBeCharArray = strToBeReplaced.ToCharArray(); 6             char[] strToCharArray = strToReplace.ToCharArray(); 7             List<Char> newCharList = new List<Char>(); 8  9             for (int i = 0; i < originalCharArray.Count(); i++)10             {11                 if (originalCharArray[i] == strToBeCharArray[0])12                 {13                     bool IsReplace = false;14                     for (int j = 0; j < strToBeCharArray.Count(); j++)15                     {16                         if (((i + j) < originalCharArray.Count())17                             && (originalCharArray[i + j] == strToBeCharArray[j]))18                         {19                             IsReplace = true;20                         }21                         else22                         {23                             IsReplace = false;24                             break;25                         }26                     }27                     if (IsReplace)28                     {29                         i += strToBeCharArray.Count() - 1;30                         for (int k = 0; k < strToCharArray.Count(); k++)31                         {32                             newCharList.Add(strToCharArray[k]);33                         }34                     }35                     else36                     {37                         newCharList.Add(originalCharArray[i]);38                     }39                 }40                 else41                 {42                     newCharList.Add(originalCharArray[i]);43                 }44             }45 46             resultString = string.Join("", newCharList);47             return resultString;48         }

 

Due to the time limit, I didn't add comments. However, the logic is simple and clear without comments. The disadvantage is that the algorithm complexity is relatively high. With my consent, I will repeat the implementation code of my colleague Hello Kitty on the same issue and use another method to solve the same problem. A little more code, some additional functions are added, and various annotations are complete. Of course, it takes more time. You are welcome to discuss this topic together. Please leave a message and comment on it! PS: Just now I found a bug in the following code. It's just a hidden egg!

1 public class Replace 2 {3 /// <summary> 4 /// Replace Method 5 /// </summary> 6 /// <param name = "source"> original string </param> 7 /// <param name = "find"> string to be searched </param> 8 /// string to be replaced by <param name = "replace"> </param> 9 // <returns> string that is successfully replaced </returns> 10 public string Replace (string source, string find, string replace) 11 {12 // the string to be searched is greater than the original string. if (find. length> source. length) 14 {15 return source; 16} 17 18 // The number of times the record finds 19 int findCount = 0; 20 // only used for marking, number of secondary records 21 bool flag = true; 22 // n: Value of source string traversal; j: Value of find string traversal 23 int n = 0, j = 0; 24 // s: Find the start index of the string, e: Find the end index of the string 25 int s = 0, e = 0; 26 27 while (true) 28 {29 // judge whether the characters are equal to 30 if (source [n] = find [j]) 31 {32 // Source sequence + 1 33 n ++; 34 // judge whether it is the first matched 35 if (j = 0) 36 {37 // assign a value to s and search for the index at the beginning 38 s = n; 39} 40 // find the next character of the next comparison to find 41 j ++; 42 // mark the previous character 43 flag = true; 44} 45 else 46 {47 // records do not fully match 48 flag = false; 49 // find indexes return to 50 j = 0; 51 // The Source index continues to add 52 n ++; 53} 54 55 // search has been completed 56 if (j = find. length) 57 {58 // exact match 59 if (flag) 60 {61 // number of characters to search for + 1 62 findCount ++; 63} 64 // record search array End index 65 e = n; 66 // source index continue + 1 67 n ++; 68 // find index to zero 69 j = 0; 70 // calculate and generate a new string, and then continue the loop until all strings 71 source = GetNewString (source, find, replace, s, e); 72} 73 // after the Source traversal is completed, exit the loop 74 if (n> = source. length) 75 {76 break; 77} 78} 79 // The final string 80 return source; 81} 82 83 // <summary> 84 // obtain the new string 85 // </summary> 86 // <param name = "source"> source string </param> 87 // <param name = "find"> the character to be searched </param> 88 // <param name = "replace"> the character to be replaced </param> 89 // <param name = "startIndex"> Start index of the searched character </param> 90 // <param name = "endIndex"> </param> 91 // <returns> return the replaced string </returns> 92 public string GetNewString (string source, string find, string replace, int startIndex, int endIndex) 93 {94 // The length of the new string is 95 int newArrayLength = source. length + endIndex-startIndex; 96 // new character array 97 char [] newStringArray = new char [newArrayLength]; 98 // copy the first half to the new string 99 for (int I = 0; I <startIndex-1; I ++) 100 {101 newStringArray [I] = source [I]; 102} 103 // the current temporary start index 104 int tempCurrentStartLength = startIndex-1; 105 // assign the value to the new character array 106 for (int I = tempCurrentStartLength; I <tempCurrentStartLength + replace. length; I ++) 107 {108 newStringArray [I] = replace [I-tempCurrentStartLength]; 109} 110 // assign the remaining characters to the new array 111 for (int I = endIndex + 1; I <newArrayLength; I ++) 112 {113 newStringArray [I] = source [I-1]; 114} 115 // return the converted string 116 return string. concat (newStringArray); 117} 118}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.