【String字符串分割以及字符串截取】

您所在的位置:网站首页 string取前几位 【String字符串分割以及字符串截取】

【String字符串分割以及字符串截取】

2023-09-25 07:30| 来源: 网络整理| 查看: 265

字符串分割以及字符串截取 字符串分割案例:运行结果 字符串截取案例:运行结果

字符串分割

1.使用String类中的split()方法可以对字符串进行分割,split()方法提供了以下两种字符串的分割形式: (1)split(**regex:**使用“具体分割条件”进行分割 **limit:**分割后结果分成几份)

案例: public static void main(String[] args) { String s = "123,456,789"; //使用split进行字符串分割,使用“,”进行分割 // String[] split = s.split(","); // String s1 = ArrayUtils.toString(split); // System.out.println(s1); //使用split进行字符串分割,(regex:使用“,”进行分割 limit:分割后结果分成2份) String[] split = s.split(",", 2); for (String s1 : split) { System.out.println(s1); } System.out.println(split.length); } 运行结果 [INFO] --- exec-maven-plugin:1.6.0:exec (default-cli) @ test --- 123 456,789 2 字符串截取

语法:字符串.substring(开始下标,结束下标) - 返回被截取的字符串,不包含结束下标对应的字符

不可以使用负数表示下标

案例: public static void main(String[] args) { //字符串截取 String str = "abcdefg"; String substring = str.substring(1, 3); System.out.println(substring); } 运行结果 [INFO] --- exec-maven-plugin:1.6.0:exec (default-cli) @ test --- bc


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3