Java String.split()特殊字符的使用

split在jdk中用法说明:


String[] split(String regex)
Splits this string around matches of the given  regular expression.

public String[] split(String regex)
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:

RegexResult
:{ "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:
Pattern

String[] split(String regex, int limit)
Splits this string around matches of the given  regular expression.

public String[] split(String regex,
             int limit)
Splits this string around matches of the given  regular expression.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

The string "boo:and:foo", for example, yields the following results with these parameters:

RegexLimitResult
:2{ "boo", "and:foo" }
:5{ "boo", "and", "foo" }
:-2{ "boo", "and", "foo" }
o5{ "b", "", ":and:f", "", "" }
o-2{ "b", "", ":and:f", "", "" }
o0{ "b", "", ":and:f" }

An invocation of this method of the form str.split(regexn) yields the same result as the expression

Pattern. compile( regex). split( strn)
Parameters:
regex - the delimiting regular expression
limit - the result threshold, as described above
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:
Pattern


split()返回是一个数组

注释:如果是符号的话,需要转义 ,例如:

用“|”作为分隔的话,必须是String.split("\\|"),这样才能正确的分隔开,不能用String.split("|");

还有String.split("\\.") String.split("\\*")等

最特殊的:以\分割String.split("\\\\")  这里面前两个是转义的,后面是两个的原因是因为字符串中是两个。

    (字符串中为什么是\\呢?因为java中字符串\会被转义的)




<think>好的,用户让我介绍一下Java中的String.split方法。首先,我得回忆一下这个方法的基本用法。记得split是用来根据正则表达式分割字符串的,返回一个字符串数组。但可能需要更详细的解释。 用户可能刚学Java,对正则表达式不太熟悉,所以得解释清楚参数的意义。比如,普通字符分割和正则表达式特殊字符的区别,比如用点号.分割的话需要转义,因为点在正则里代表任意字符。 接下来,应该分点说明。首先是基本用法,比如split(",")分割逗号分隔的字符串。然后是有多个分隔符的情况,比如用正则的管道符|,例如split("a|b")。然后是限制分割次数的参数,第二个参数的作用,比如split(":", 3)会分成最多三个部分。 还需要提到一些常见的坑,比如分割点号或者星号的时候需要转义,或者处理多个空格的情况可以用\\s+。另外,空字符串的情况需要注意,比如分割后开头或结尾有空元素的话,split默认会处理掉,但如果有limit参数的话可能保留。 可能用户在实际使用中会遇到问题,比如分割后的数组长度,或者特殊字符的处理。应该举几个例子,比如用逗号分割,或者用正则表达式分割数字和字母。另外,提到limit参数为0、正数、负数时的不同行为,比如负数会保留空元素,而0会去掉末尾的空元素。 还需要注意不同版本的Java是否有差异,不过可能大部分情况下行为一致。最后总结一下常用的场景和注意事项,让用户能正确使用这个方法,避免常见的错误。</think>Java中的`String.split()`方法用于将字符串按照指定的正则表达式规则进行分割。以下是逐步说明: ### 一、基本用法 ```java String str = "apple,banana,orange"; String[] fruits = str.split(","); // 结果:["apple", "banana", "orange"] ``` - 参数:正则表达式(如`,`) - 返回:分割后的字符串数组 ### 二、正则表达式分割 需注意特殊字符需转义: ```java String str = "192.168.1.1"; String[] nums = str.split("\\."); // 转义点号 // 结果:["192", "168", "1", "1"] ``` ### 三、多分隔符 用`|`表示逻辑或: ```java String str = "a-b+c_d"; String[] parts = str.split("[-+_]"); // 结果:["a", "b", "c", "d"] ``` ### 四、限制分割次数(limit参数) ```java String str = "one:two:three:four"; String[] parts1 = str.split(":", 2); // 分割2-1次 // 结果:["one", "two:three:four"] String[] parts2 = str.split(":", -1); // 保留所有空元素 ``` ### 五、常见场景处理 1. **分割空格**: ```java String str = "Hello World Java"; String[] words = str.split("\\s+"); // 匹配任意多个空格 // 结果:["Hello", "World", "Java"] ``` 2. **空字符串处理**: ```java String str = ",a,b,,c,"; String[] arr1 = str.split(","); // 默认去除末尾空值 // 结果:["", "a", "b", "", "c"] String[] arr2 = str.split(",", -1); // 保留所有空值 // 结果:["", "a", "b", "", "c", ""] ``` ### 六、注意事项 1. 正则表达式需正确转义 2. 默认会去除末尾空元素 3. `limit=0`时效果与默认相同 4. 空字符串分割返回长度为1的数组(包含原字符串) 建议通过调试工具查看分割结果,确保符合预期。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值