Home > Java > JavaBase > What is the use of split() method in java

What is the use of split() method in java

青灯夜游
Release: 2023-01-13 00:39:54
Original
9719 people have browsed it

在java中,split()方法可以根据匹配给定的正则表达式来拆分字符串,然后返回一个字符串数组,语法“stringObject.split(正则表达式分隔符, 分割的份数)”;如果有多个分隔符,可以用“|”作为连字符。

What is the use of split() method in java

本教程操作环境:windows7系统、java8版、DELL G3电脑。

定义和用法

split() 方法用于把一个字符串分割成字符串数组。

语法

stringObject.split(separator,howmany)
Copy after login
参数描述
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]);
Copy after login
  • String address="上海|上海市|闵行区|吴中路";
String[] splitAddress=address.split("\\|"); //如果以竖线为分隔符,则split的时候需要加上两个斜杠【\\】进行转义
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login
  • String address="上海*上海市*闵行区*吴中路";
String[] splitAddress=address.split("\\*");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login
  • String address="上海:上海市:闵行区:吴中路";
String[] splitAddress=address.split("\\:");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login
  • String address="上海.上海市.闵行区.吴中路";
String[] splitAddress=address.split("\\.");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login
  • String address="上海^上海市^闵行区^吴中路";
String[] splitAddress=address.split("\\^");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login
  • String address="上海@上海市@闵行区@吴中路";
String[] splitAddress=address.split("@");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login
  • String address="上海,上海市,闵行区,吴中路";
String[] splitAddress=address.split(",");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login

2、多个符号作为分隔符

String address="上海^上海市@闵行区#吴中路";

String[] splitAddress=address.split("\\^|@|#");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
Copy after login

例:

 String address = new String("192.168.13.240");
        String[] str = address.split("\\.");
        for(String s : str){
            System.out.println(s);
        }
Copy after login

输出格式:

192
168
13
240
Copy after login

总结:

(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!

Related labels:
source:php.cn
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