Java——String.split()函数
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
The string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result : { "boo", "and", "foo" } o { "b", "", ":and:f" }
- Parameters:
- regex the delimiting regular expression
- Returns:
- the array of strings computed by splitting this string around matches of the given regular expression
- Throws:
- PatternSyntaxException - if the regular expression's syntax is invalid
- Since:
- 1.4
- See Also:
- java.util.regex.Pattern
- @spec
- JSR-51
- 但是要注意以下几点:
-
(1)如果用“.”作为分隔的话必须是如下写法String.split("\\."),这样才
能正确的分隔开不能用String.split(".");
比如这段代码:
String str="123.456";
String []arrStr=str.split(".");
System.out.println(arrStr.length);结果为:

而:
-
String str="123.456";
String []arrStr=str.split("\\.");
System.out.println(arrStr.length);结果为:

原因是
Splits this string around matches of the given regular expression.要求的是一个正则表达式所以必须满足正则表达式的形式,
因此需要进行转义才行。
下面给出split()函数的源代码供参考:
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else { // last one
//assert (list.size() == limit - 1);
list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return this
if (off == 0)
return new String[]{this}; // Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length)); // Construct result
int resultSize = list.size();
if (limit == 0)
while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
resultSize--;
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}public String[] split(String regex) {
return split(regex, 0);
}
Java——String.split()函数的更多相关文章
- java String.split()函数的用法分析
java String.split()函数的用法分析 栏目:Java基础 作者:admin 日期:2015-04-06 评论:0 点击: 3,195 次 在java.lang包中有String.spl ...
- 【转】java String.split()函数的用法分析
在java.lang包中有String.split()方法的原型是: public String[] split(String regex, int limit) split函数是用于使用特定的切 ...
- [java]java String.split()函数的用法分析
转自:http://swiftlet.net/archives/709 一.在java.lang包中有String.split()方法的原型是: public String[] split(Strin ...
- Java String.split()函数分隔回车注意事项
作者:Sun1956 原文:https://blog.csdn.net/sun1956/article/details/45096117 --------------------- 我们在Java中如 ...
- Java String.split()小点
java String.split(); 别的不说,单说其中一个问题,这个函数去切分空字符串时,得到的结果: public static void main(String[] args) {// St ...
- String.Split()函数
我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),当中用到了String.SPl ...
- String.Split()函数 多种使用实例
我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),其中用到了String.SPl ...
- Array和String测试与java.String.split
java.string.split() 存在于java.lang包中,返回值是一个数组. 作用是按指定字符或者正则去切割某个字符串,结果以字符串数组形式返回. 例 String [] toSort = ...
- Java String Split Method
Java String.split() method 有如下几种特殊情况: 1. 分隔符出现在首尾 public static void main(String args[]) { String St ...
随机推荐
- leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)
https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...
- ORA-02041: client database did not begin a transaction
.NET中访问Oracle数据库链接:ORA-02041: client database did not begin a transaction 问题的处理. .NET中访问Oracle中带有DB_ ...
- SQL2008-表对表直接复制数据
1.全部复制,使用简单,但是字段容易出错(字段和顺序必须相同) INSERT INTO AAAStuffAgitationYield SELECT * FROM StuffAgitationYiel ...
- 【Stage3D学习笔记续】真正的3D世界(四):空间大战雏形
前面几个星期抽空用Starling做了一个打飞机的小游戏(所以没有接着看书了),准备面试时用的,结果面试还是没过%>_<%...这个游戏打算过几天全部开源了 那么接下来打算这周把<S ...
- C++学习笔记(三):数组
数组声明时必须指定该数组的长度: ]; 这个时候已经分配了内存,但没有初始化,所以具体的值是不确定的: 初始化: ] = {, , }; ] = {};//指定第一个数字为1,后面的使用0填充: ] ...
- Azure 云服务中的实例端点
Azure云服务(cloud Servive)中有三种端点类型(endpoint type):输入端点(input);内部端点(internal);实例端点(InstanceInput) 1.输入端点 ...
- OpenCV训练分类器制作xml文档
OpenCV训练分类器制作xml文档 (2011-08-25 15:50:06) 转载▼ 标签: 杂谈 分类: 学习 我的问题:有了opencv自带的那些xml人脸检测文档,我们就可以用cvLoad( ...
- 教你50招提升ASP.NET性能(四):精选的技巧
(4)A selection of tips 招数4: 精选的技巧 Make sure HTTP compression is turned on for any uncompressed conte ...
- JS获取客户端电脑信息(转)
<html> <head> <title></title> <script type="text/javascript"> ...
- ThinkPHP CURD方法盘点:order方法
order方法属于模型的连贯操作方法之一,用于对操作的结果排序. 用法 $Model->where('status=1')->order('id desc')->limit(5)-& ...