Examples of two methods to split strings in Java

preface

I believe everyone should know that in Java programming, sometimes we need to divide a string according to a specific character, letter, etc. as the cut-off point, so that we can use part of the string or save all the intercepted contents to several groups. The following article will share two segmentation methods. Let's have a look.

1、 Java Split() method of lang. string, JDK 1.4 or later

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

Sample code

Output results:

The split implementation directly calls the split method of the matcher class. When using string When the split method separates strings, if some special characters are used in the separator, we may not get the expected results.

Characters with special meanings in regular expressions must be escaped when we use them, for example:

Split separator summary

1. The characters "|", "*", "+" must be preceded by the escape character "\ \".

2. If it is "\", it must be written as "\ \ \ \".

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>