The Java string.split (String regex) method sets multiple delimiters __java

Source: Internet
Author: User

We know that a string variable has a split method that is used to split a string string and return an array of string literals.
For example:

String str = "Hello,boy,what ' your name?";

At this point we need to use the comma (,) as the delimiter, the sentence is cut open, then using:

string[] Array = Str.split (",");

However, sometimes this happens:

String str = "1+2-3+5-64*25+25/65";

We need to use the + 、-、 *,/as the separator, cut the string, remove the operator, get the number.
Of course, there are also solutions, and we can use regular expressions to set multiple delimiters for the split method.
For example:

String str = "1+2-3+5-64*25+25/65";
string[] Array = str.split ("\\+|-|\\*|/");

That is, use | To connect our multiple delimiters (\ = escape character, and some characters need to be escaped).

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.