Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
The regular expression can be used to construct the front edge of the string to be searched (? (I), which indicates that it is case insensitive. For example, to search for here, the structure (? I) Here, which indicates here, and so on. However, this will also match there. We can also add a delimiter to indicate a separate here word,/B (? I) Here/B. Use the string matches () method for matching. The replaceall () method is used for replacement.
The preceding two methods handle exceptions. This is ignored in the small example below.
// Ignore case differences when searching
// Or replacing substrings.
Class ignorecasedemo {
Public static void main (string ARGs []) {
String STR = "this is a test .";
System. Out. println ("ignore case when searching./N" +
"Looking for 'test' in:" + Str );
// Use matches () to find any version of test.
If (Str. Matches ("(? I). * test .*"))
System. Out. println ("test is in the string .");
System. Out. println ();
STR = "Alpha Beta, Alpha Beta ";
// Use replaceall () to ignore case when replacing one
// Substring with another.
// In this example, replace all versions of alpha with ETA.
System. Out. println ("ignore case when replacing./N" +
"Replace any version of 'alpha'" +
"With 'za' in:/N" + "" + Str );
String result = Str. replaceall ("(? I) Alpha "," ETA ");
System. Out. println ("after replacement:/N" +
"" + Result );
}
}
Of course, this is not the only method. The regionmatches () method can also be used. If it is not too troublesome, You can first convert the string to lowercase, and then use the contains () method to determine whether a substring exists.
To replace the first appearance, use the replacefirst () method.
Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/