Home > Java > javaTutorial > How to use the split method in Java String

How to use the split method in Java String

WBOY
Release: 2023-05-02 09:37:06
forward
1190 people have browsed it

String中split方法使用

String的split()方法用于按传入的字符或字符串对String进行拆分,返回拆分之后的数组。

1、一般用法

用一般的字符,例如@或,等符号做分隔符时:

String address="上海@上海市@闵行区@吴中路";
String[] splitAddr=address.split("@");
System.out.println(splitAddr [0]+splitAddr [1]+splitAddr [2]+splitAddr [3]);
Copy after login

2、需要转义的分隔符

当使用* ^ : | . \等6个符号做分隔符时,上述6个符号转义字符,必须得加"\\",即split(“\\^”)等。第二个“\”是用来给这6种符号转义,第一个“\”是用来给第二个“\”转义。

String address="上海*上海市*闵行区*吴中路";
String[] splitAddr=address.split("\\*");  
System.out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3]);
Copy after login

其中有个更特殊的,就是“\”。如果字符串中想要使用"\",就应该使用"\\"进行转义。也就是说,对于"a\b",应该写成"a\\b",而如果想要用split方法针对"\"进行拆分,应该使用"a\\b".split("\\\\")。

3、多个符号作为分隔符

可以用“|”字符作为连字符,把多个分隔符分隔的内容都区分开:

String address="上海^上海市@闵行区#吴中路";
String[] splitAddr=address.split("\\^|@|#");
System.out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3]);
Copy after login

4、空值的存储

如果split(String s)函数产生了空值,那么不会存到数组中。可以通过使用它的重载函数split(";",-1)实现空值的保存。这里的“;”只是作为分隔符的一个例子。

String.split()需要的转义字符

注意“/”和“-”,"&"不是转义字符。

String str="aaaa/aaaa/aaaa";
String[] strings=str.split("/");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa-aaaa-aaaa";
String[] strings=str.split("-");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa&aaaa&aaaa";
String[] strings=str.split("&");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login

转义字符

  • |

  • \

  • $

  • *

  • +

  • .

  • ?

  • ^

  • (

  • )

  • [

  • ]

  • {

  • }

String str="aaaa|aaaa|aaaa";
String[] strings=str.split("\\|");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa\\aaaa\\aaaa";
String[] strings=str.split("\\\\");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa$aaaa$aaaa";
String[] strings=str.split("\\$");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa*aaaa*aaaa";
String[] strings=str.split("\\*");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa+aaaa+aaaa";
String[] strings=str.split("\\+");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa.aaaa.aaaa";
String[] strings=str.split("\\.");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa?aaaa?aaaa";
String[] strings=str.split("\\?");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa^aaaa^aaaa";
String[] strings=str.split("\\^");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login
String str="aaaa(aaaa(aaaa";
String[] strings=str.split("\\(");
for (int i = 0; i < strings.length; i++) {
    System.out.println(strings[i]);
}
Copy after login

所有的括号都是转义字符。 

The above is the detailed content of How to use the split method in Java String. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template