Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to split String strings in Java

Shulou Source: shulou.com Published: 2022-05-31 11:01:20 10月05日 Update

This article mainly introduces "how to split String strings in Java". In daily operation, I believe many people have doubts about how to split String strings in Java. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to split String strings in Java". Next, please follow the editor to study!

Usage

One declaration of the split method is

Public String [] split (String regex)

Regex refers to the regular expression delimiter. We usually use a single character as the delimiter, which can actually be regarded as a special regular expression. The special feature is that this expression only matches itself, such as "-" match only "-". Examples are as follows:

String string = "86-15003455666"; String [] parts = string.split ("-"); String part1 = parts [0]; / 86 String part2 = parts [1]; / 15003455666

Another declaration of the split method is:

Public String [] split (String regex, int limit)

Regex refers to the regular expression delimiter, while limit specifies the number of split copies. For example, you can see

String string = "004-556-42"; String [] parts = string.split ("-", 2); / / Limited division of two copies String part1 = parts [0]; / 004 String part2 = parts [1]; / 556-42

In some scenarios, we may want to keep the delimiter in the result, which is also possible to connect the delimiter to the result on the left side of the split.

String string = "86-15003455666"; String [] parts = string.split ("(?

Tags: Characters expressions strings regular delimiters methods learning median numbers results special two representatives right scene objects more formatting help practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Microsoft Linux macOS