使用split()方法可以使字符串按指定的分割字符或字符串对内容进行分割,并将分割后的结果存放在字符串数组中。split()方法提供了以下两种重载形式。

(1)split(String sign)

其中,sign为分割字符串的分割符对字符串进行拆分;

注:没有统一的对字符进行分割的符号。如果想定义多个分隔符,可以使用符号“|”。例如:“,|=”表示分隔符分别为“,”和“=”。

(2)split(String sign,int limit)

limit表示限制的分割次数

public class Gess {public static void main(String[] args) throws Exception {String str = new String("abc,de=f,ghi,gkl");String newstr [] = str.split(",");                //使用split()方法对字符串进行拆分,返回字符串数组for (int i = 0; i < newstr.length; i++) {        //使用for循环遍历数组System.out.println(newstr[i]);                //输出信息
        }System.out.println("================================");String newstr1 [] = str.split(",|=");                //使用split()方法对字符串进行拆分,返回字符串数组for (int i = 0; i < newstr1.length; i++) {        //使用for循环遍历数组System.out.println(newstr1[i]);                //输出信息
        }System.out.println("================================");//对字符串进行拆分,并限定拆分次数,返回字符数组String[] newstr2 = str.split(",", 2);for (int i = 0; i < newstr2.length; i++) {System.out.println(newstr2[i]);}System.out.println("================================");String[] newstr3 = str.split(",", 0);for (int i = 0; i < newstr3.length; i++) {System.out.println(newstr3[i]);}System.out.println("================================");String[] newstr4 = str.split(",", -2);for (int i = 0; i < newstr4.length; i++) {System.out.println(newstr4[i]);}System.out.println("================================");String[] newstr5 = str.split(",", 5);for (int i = 0; i < newstr5.length; i++) {System.out.println(newstr5[i]);}}}

输出结果:

abc
de=f
ghi
gkl
================================
abc
de
f
ghi
gkl
================================
abc
de=f,ghi,gkl
================================
abc
de=f
ghi
gkl
================================
abc
de=f
ghi
gkl
================================
abc
de=f
ghi
gkl

转载于:https://www.cnblogs.com/xiaotao520/p/9204302.html

字符串拆分 split()方法相关推荐

  1. sqlserver字符串拆分(split)方法汇总

    declare @s varchar(100),@sql varchar(1000) set @s='1,2,3,4,5,6,7,8,9,10,11' while(charindex(',',@s)& ...

  2. Java字符串之split方法

    文章目录 1 String[] split(String regex) 2 String[] split(String regex, int limit) 3 split()的应用 3.1 leetc ...

  3. 字符串的split方法(),把字符串分割为数组

    // split()方法 // 作用:使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置(从哪开始分割) // 返回值:分割形成的数组 例子:   ...

  4. 字符串的split方法

    作用 split() 方法用于把一个字符串分割成字符串数组. 参数 描述 stringObject.split(separator,howmany) separator 必需.字符串或正则表达式,从该 ...

  5. JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)...

    字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...

  6. 字符串使用split()方法截取时的空字符串问题

    一.用例子来说明 1.单个分隔符占据第1个位置 @Test public void test() {String str = "_1";String[] s = str.split ...

  7. mysql字符串拆分split功能_mysql字符串拆分实现split功能

    转自:https://blog.csdn.net/pjymyself/article/details/81668157 有分隔符的字符串拆分 [0]需求 数据库中 num字段值为: 实现的效果:需要将 ...

  8. js 字符串切割 split方法 遍历map方法

    split: var str='123456';console.log(str.split(''))// 打印数据 ["1","2","3" ...

  9. 字符串分割split()方法:将一个字符串通过指定的分隔符分割成若干子串

    11111 将这行数据存为字符串,然后调用字符串分割函数split将其分割成字符数组String [] strRating,在定义一个整型数组,将字符数组转换成整数数组: import java.ut ...

最新文章

  1. [导入]Java线程的深入探讨
  2. 取一个字符串中的数字
  3. Android运行时异常“Binary XML file line # : Error inflat
  4. python强大体现在哪些方面-什么python的if语句?它主要应用在哪些方面?
  5. OpenCV纠正文本中的偏斜(附完整代码)
  6. SQL2008 收缩日志和数据脚本
  7. python有大括号吗_只有我一个人觉得Python取消了大括号而显得结构更加混乱了吗?...
  8. 第五十九期:商用数据库之死:Oracle 面临困境
  9. 管道( Pipeline )模型--示例
  10. 程序员应如何理解高并发中的协程
  11. 怎么看调用的接口_SpringCloud服务间调用
  12. svn如何取消某个文件的版本管理_微服务架构如何统一管理工程配置文件
  13. Spring+hibernate+JSP实现Piano的数据库操作---1.目录结构+展示
  14. php 扩展jpeg,php重新编译,gd扩展支持jpeg文件-Go语言中文社区
  15. AtCoder ARC 076D - Built?
  16. H264的RTP负载打包的数据包格式,分组,分片
  17. thymeleaf select 回显
  18. 软件测试中的可用性、可维护性、可靠性有什么区别?
  19. 华为方会提供一份CRS(客户需求)和SOW(工作任务书)
  20. 初测ONES项目管理工具,寻求与众不同

热门文章

  1. Java两大数据类型
  2. 关于PL/SQL我写了一份从0到1的入门教程
  3. 概念模型、逻辑模型和物理模型的区别
  4. 2022高职大数据竞赛0720更新参考实现
  5. VUE环境搭建和项目创建-win平台
  6. [算法总结] 中位数+绝对值不等式 AcWing 104. 货仓选址
  7. Sign in with Apple(object-c) 从开发者后台到服务端验证
  8. T045基于51单片机温湿度检测系统无线蓝牙传输Proteus仿真原理图PCB
  9. 如何将css文件引入到项目当中
  10. java验证歌德巴赫猜想