public String[] split(String regex) 默认limit为0

public String[] split(String regex, int limit)

当limit>0时,则应用n-1次

  1. public static void main(String[] args) {
  2. String s = "boo:and:foo";
  3. String[] str = s.split(":",2);
  4. System.out.print(str[0] + "," + str[1]);
  5. }

结果:

  1. boo,and:foo

当limit<0时,则应用无限次

  1. public static void main(String[] args) {
  2. String s = "boo:and:foo";
  3. String[] str = s.split(":",-2);
  4. for(int i = 0 ; i < str.length ; i++){
  5. System.out.print(str[i] + " ");
  6. }
  7. }

结果:

  1. boo and foo

当limit=0时,应用无限次并省略末尾的空字符串

  1. public static void main(String[] args) {
  2. String s = "boo:and:foo";
  3. String[] str = s.split("o",-2);
  4. for(int i = 0 ; i < str.length ; i++){
  5. if( i < str.length - 1)
  6. System.out.print("(" + str[i] + "),");
  7. else
  8. System.out.print("(" + str[i] + ")");
  9. }
  10. }

结果:

  1. (b),(),(:and:f),(),()
  1. public static void main(String[] args) {
  2. String s = "boo:and:foo";
  3. String[] str = s.split("o",0);
  4. for(int i = 0 ; i < str.length ; i++){
  5. if( i < str.length - 1)
  6. System.out.print("(" + str[i] + "),");
  7. else
  8. System.out.print("(" + str[i] + ")");
  9. }
  10. }

结果:

  1. (b),(),(:and:f)

2016/09/21 java split用法的更多相关文章

  1. 2016/09/21 java关键字static

    1.static方法     static方法一般称作静态方法,由于静态方法不依赖于任何对象就可以进行访问,因此对于静态方法来说,是没有this的,因为它不依附于任何对象,既然都没有对象,就谈不上th ...

  2. 2016/09/21 Java关键字final

    1.final类 final类不能被继承,没有子类,final类中的方法默认是final的. final类不能被继承,因此final类的成员方法没有机会被覆盖,默认都是final的. 2.final方 ...

  3. Java split用法

    Java split用法 java.lang.string.split split 方法  将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separat ...

  4. Java split()用法

    特殊情况有 * ^ : | . \ 一.单个符号作为分隔符  String address="上海\上海市|闵行区\吴中路"; String[] splitAddress=addr ...

  5. 【转发】Java split()用法

    特殊情况有 * ^ : | . \ 一.单个符号作为分隔符  String address="上海\上海市|闵行区\吴中路"; String[] splitAddress=addr ...

  6. TFS Training for Kunlun bank (http://www.klb.cn/) 微软研发流程(ALM)管理培训会议(昆仑银行) 2016.09.21

    银行一直是微软技术的伤心地,由于历史原因,微软技术和产品一直很难进入到银行业务的核心区域,但是微软今年来的进步不少,在开发工具和平台方面已经连续攻克了几个典型的金融企业,例如农业银行,中国人保等. 应 ...

  7. 2016/10/21 java中的参数传方式

    参考:http://blog.sina.com.cn/s/blog_59ca2c2a0100qhjx.html http://www.cnblogs.com/caiyao/p/4964176.html

  8. 2016/09/21 context.getConfiguration().get()

    查看api:http://hadoop.apache.org/docs/stable/api/ public String get(String name) Get the value of the ...

  9. 2016/1/18 Java开发中的23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

随机推荐

  1. Unity小厨房之-----背后视角摄像机

    相信每一个接触过Unity的人,应该都认识Unity引擎自带的那个水管工,也一定知道那个小人模型上挂着3个脚本控制着小人的一切,今天我就来说一下关于Unity背后视角摄像机的实现,当然方法并不唯一,这 ...

  2. Redis集群创建报错

    Redis集群环境:och163/och164/och165 在执行如下脚本时报错: ./src/redis-trib.rb create 10.1.253.163: 10.1.253.164: 10 ...

  3. 001 The Hello World In Csharp

    C#是面向对象编程语言,语法和JAVA非常相似.接下来让我们看一下C#的Hello world. //001.cs using System; public class Hello { public ...

  4. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. Spark Job Scheduling

    最近由于项目需要在研究spark相关的内容,形成了一些技术性文档,发布这记录下,懒得翻译了. There are some spaces the official documents didn't e ...

  6. 慕课网-安卓工程师初养成-2-10 Java中的强制类型转换

    来源:http://www.imooc.com/code/1241 相信小伙伴们也发现了,尽管自动类型转换是很方便的,但并不能满足所有的编程需要. 例如,当程序中需要将 double 型变量的值赋给一 ...

  7. 【练习】手工生成awr报告

    ①首先进入$ORACLE_HOME/rdbms/admin目录 [oracle@host02 ~]$ cd $ORACLE_HOME/rdbms/admin 该目录下 [oracle@host02 a ...

  8. Swift学习(三)类型推导&基本运算&分支&循环

    一.Swift中类型推导&基本运算 Swift中类型推导 如果一个标识符在定义时有直接赋值,那么可以根据后面赋值的类型,来推导出前面标识符的类型,这样前面标识符的(:类型)可以省略 查看标识符 ...

  9. 免费的API接口

    有如下三个Json格式的查询天气预报接口: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/dat ...

  10. math对象和date对象

    math对象的函数方法,记住Math首字母要大写 console.log(Math.abs(-5)); //取绝对值 console.log(Math.ceil(1.1)); //向上取舍 conso ...