Software Test learning Note Week 4---processing of strings

Source: Internet
Author: User

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

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.