How to use the split method in Java String
String中split方法使用
String的split()方法用于按传入的字符或字符串对String进行拆分,返回拆分之后的数组。
1、一般用法
用一般的字符,例如@或,等符号做分隔符时:
String address="上海@上海市@闵行区@吴中路"; String[] splitAddr=address.split("@"); System.out.println(splitAddr [0]+splitAddr [1]+splitAddr [2]+splitAddr [3]);
2、需要转义的分隔符
当使用* ^ : | . \等6个符号做分隔符时,上述6个符号转义字符,必须得加"\\",即split(“\\^”)等。第二个“\”是用来给这6种符号转义,第一个“\”是用来给第二个“\”转义。
String address="上海*上海市*闵行区*吴中路"; String[] splitAddr=address.split("\\*"); System.out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3]);
其中有个更特殊的,就是“\”。如果字符串中想要使用"\",就应该使用"\\"进行转义。也就是说,对于"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]);
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]); }
String str="aaaa-aaaa-aaaa"; String[] strings=str.split("-"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa&aaaa&aaaa"; String[] strings=str.split("&"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
转义字符
|
\
$
*
+
.
?
^
(
)
[
]
{
}
String str="aaaa|aaaa|aaaa"; String[] strings=str.split("\\|"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa\\aaaa\\aaaa"; String[] strings=str.split("\\\\"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa$aaaa$aaaa"; String[] strings=str.split("\\$"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa*aaaa*aaaa"; String[] strings=str.split("\\*"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa+aaaa+aaaa"; String[] strings=str.split("\\+"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa.aaaa.aaaa"; String[] strings=str.split("\\."); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa?aaaa?aaaa"; String[] strings=str.split("\\?"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa^aaaa^aaaa"; String[] strings=str.split("\\^"); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
String str="aaaa(aaaa(aaaa"; String[] strings=str.split("\\("); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]); }
所有的括号都是转义字符。
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
