search
HomeJavajavaTutorialHow 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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does platform independence benefit enterprise-level Java applications?How does platform independence benefit enterprise-level Java applications?May 03, 2025 am 12:23 AM

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

What are the benefits of Java's platform independence for developers?What are the benefits of Java's platform independence for developers?May 03, 2025 am 12:15 AM

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

What are the advantages of using Java for web applications that need to run on different servers?What are the advantages of using Java for web applications that need to run on different servers?May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?May 02, 2025 am 12:25 AM

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

How do newer versions of Java address platform-specific issues?How do newer versions of Java address platform-specific issues?May 02, 2025 am 12:18 AM

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

Explain the process of bytecode verification performed by the JVM.Explain the process of bytecode verification performed by the JVM.May 02, 2025 am 12:18 AM

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.