Occasionally found Dongdong, do not remember is that the Bolg. Very good article ....
§1 Dark Years
There is a string, how do I find out if there are Y and F characters? The darkest way is to:
Program 1: I know if, for statements, and Charat ().
Class test{
public static void Main (String args[]) {
String str= "For my money, the important thing" +
"About the meeting is bridge-building";
Char x= ' y ';
Char y= ' f ';
Boolean result=false;
for (int i=0;i<str.length (); i++) {
Char Z=str.charat (i); System.out.println (z);
if (x==z| | Y==z) {
Result=true;
Break
}
else Result=false;
SYSTEM.OUT.PRINTLN (result);
}
}
It seems intuitive, but it's hard to cope with complicated work. If you are querying a paragraph of text, is it? Whether there are thing or ting. It's a nasty job.
§2 Java Java.util.regex Package
In accordance with object-oriented thinking, it is more natural to encapsulate a string of queries such as is, thing, or ting into an object, and to match a text with this object as a template. The same thing as a template is the regular expression that is discussed below. Let's not consider that complicated, look at an example:
Program 2: Do not understand. Let's see what we can do.
Import java.util.regex.*;
Class regex1{
public static void Main (String args[]) {
String str= "For my money, the important thing" +
"About the meeting is bridge-building";
Pattern P=pattern.compile (regEx);
Matcher M=p.matcher (str);
Boolean result=m.find ();
SYSTEM.OUT.PRINTLN (result);
}
}