When we work with strings, we sometimes encounter situations where the Integer.parseint () function is thrown when the input string is not all composed of numbers
When we encounter a similar situation, we can use regular expressions to determine if the input string matches the pattern we need, and we need to use the match () method of the String class.
The syntax for this method definition is as follows:
PublicBoolean matches(String regex)
This method tells whether the string matches the given regular expression. Form Str.matches (regex) The invocation of this method produces exactly the same result as the expression pattern.matches (Regex, str).
If this method returns True, if and only if the string specified by the regular expression matches.
As for the use of specific regular expressions, this blog is very detailed and http://blog.csdn.net/amaowolf/article/details/7758215.
For example, if we want the user to enter an integer between 6 bits and 0-9, you can use the matching string "\\d{6}",
The test results are as follows
Part of the test code is the following public class Stringprocess {
public static void Main (string[] args) {
String a = args[0];
if (a.length () > 0 &&!a.matches ("\\d{6}"))
System.out.println ("Please enter an integer between 6 bits 0-6");
else {
int B = Integer.parseint (a);
System.out.println (b);
}
}
}
Software Test learning Note Week 4---processing of strings