Java God -- How to use split in Java?

Copyright © 2019 SheepCore Authentic Articles.

If you wanna reblog the article, please mark the provenience:

https://mp.youkuaiyun.com/postedit/87281432.

1. Function:  Splits this string around matches of the given regular expression.

2.Pattern:  stringDemo.split(String regex, int limit);

                   -- regex shows how to seperate.

                   -- limit is the length of seperated array of strings.

3.Examples:

example 1:

String str = "SheepCore@leetcode@";
String[] arrOfStr = str.split("@");

output:

   SheepCore

    leetcode

 

example 2:  

String str = "SheepCore@leetcode@";
String[] arrOfStr = str.split("@",1);
System.out.println("The length of array: "+ arrOfStr.length);

output:

SheepCore@leetcode@

The length of array: 1

 

example 3:

 

String str = "SheepCore@leetcode@";
String[] arrOfStr = str.split("@", 0);

output:

SheepCore

leetcode

The length of array: 2     //limit=0时,忽略尾部分隔符,所以尾部分隔出的空串被忽略掉了。

example 4:

 

String str = "SheepCore@leetcode@";
String[] arrOfStr = str.split("@", -1);

output:

SheepCore

leetcode

 

The length of array: 3  //当limit<0时,进行无限次分割,不忽略尾部分隔符。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值