The source code translation tool is basically complete, with only the last question left: How to ignore the case sensitivity of the original text when replacing the string. Because string. Replace in C # cannot ignore case sensitivity. The first thing that comes to mind is regular expressions. I checked them online and used regular expressions in combination with some logic operations to replace strings without case sensitivity. However, the regular expression method is difficult to use, and experiments prove that the speed is not the fastest. What I want to talk about is the most convenient to use, and the execution speed is also the fastest. Is to use strings in the Microsoft. VisualBasic namespace.
First, add the reference Microsoft. VisualBasic. dll
Introduce the namespace using Microsoft. VisualBasic;
Use the replace method. The following parameters are used:
Strings. replace (content of the original string, content of the field to be replaced, content of the replaced field, starting from the nth digit (note that the default value is 1 ), number of replicas (-1 indicates all), Case Insensitive );
Example:
String STR = "aabbcc_aabbcc ";
// Replace the first BB in the original string with dd
STR = strings. Replace (STR, "BB", "DD", 1, 1, comparemethod. Binary );
Printed result: aaddcc_aabbcc
(Comparemethod. Binary indicates that binary is used for searching. Because the binary code of uppercase and lowercase letters is obviously different, it is case-insensitive)
// Replace all the BB strings with dd
STR = strings. Replace (STR, "BB", "DD", 1,-1, comparemethod. Binary );
Printed result: aaddcc_aaddcc
// Replace all BB values in the original string with dd to ignore case sensitivity.
STR = strings. Replace (STR, "BB", "DD", 1,-1, comparemethod. Text );
Printed result: aaddcc_aaddcc
(Comparemethod. text means to use text to search, so it is case-insensitive)
This method only needs to pass in several important parameters, which is very convenient to use. Because the principle is to split () first and then join (), the speed is very fast. It is much faster than the regular expression method.
Note that replace cannot replace null strings. For example
String STR = "";
If you replace strings. Replace, null is returned.
STR = strings. Replace (STR, "", "test", 1,-1, comparemethod. Binary );
STR = NULL true