What is the use of split() method in java
在java中,split()方法可以根据匹配给定的正则表达式来拆分字符串,然后返回一个字符串数组,语法“stringObject.split(正则表达式分隔符, 分割的份数)”;如果有多个分隔符,可以用“|”作为连字符。
本教程操作环境:windows7系统、java8版、DELL G3电脑。
定义和用法
split() 方法用于把一个字符串分割成字符串数组。
语法
stringObject.split(separator,howmany)
参数 | 描述 |
---|---|
separator | 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。 |
howmany | 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。 |
返回值
一个字符串数组。该数组是通过在 separator 指定的边界处将字符串 stringObject 分割成子串创建的。返回的数组中的字串不包括 separator 自身。
但是,如果 separator 是包含子表达式的正则表达式,那么返回的数组中包括与这些子表达式匹配的字串(但不包括与整个正则表达式匹配的文本)。
提示和注释
注释:如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割。
注释:String.split() 执行的操作与 Array.join 执行的操作是相反的。
特殊情况有 * ^ : | . \
1、单个符号作为分隔符
- String address="上海\上海市|闵行区\吴中路";
String[] splitAddress=address.split("\\"); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
- String address="上海|上海市|闵行区|吴中路";
String[] splitAddress=address.split("\\|"); //如果以竖线为分隔符,则split的时候需要加上两个斜杠【\\】进行转义 System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
- String address="上海*上海市*闵行区*吴中路";
String[] splitAddress=address.split("\\*"); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
- String address="上海:上海市:闵行区:吴中路";
String[] splitAddress=address.split("\\:"); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
- String address="上海.上海市.闵行区.吴中路";
String[] splitAddress=address.split("\\."); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
- String address="上海^上海市^闵行区^吴中路";
String[] splitAddress=address.split("\\^"); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
- String address="上海@上海市@闵行区@吴中路";
String[] splitAddress=address.split("@"); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
- String address="上海,上海市,闵行区,吴中路";
String[] splitAddress=address.split(","); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
2、多个符号作为分隔符
String address="上海^上海市@闵行区#吴中路";
String[] splitAddress=address.split("\\^|@|#"); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
例:
String address = new String("192.168.13.240"); String[] str = address.split("\\."); for(String s : str){ System.out.println(s); }
输出格式:
192 168 13 240
总结:
(1)split表达式,其实就是一个正则表达式。* ^ | 等符号在正则表达式中属于一种有特殊含义的字符,如果使用此种字符作为分隔符,必须使用转义符即\\加以转义。
(2)如果使用多个分隔符则需要借助 | 符号,如二所示,但需要转义符的仍然要加上分隔符进行处理
相关视频教程推荐:Java视频教程
The above is the detailed content of What is the use of split() method in java. 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
