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:

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:
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:

Regex Limit Result
: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "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中字符串\会被转义的)

时间: 2024-09-10 09:26:35

Java String.split()特殊字符的使用的相关文章

Java中String.split()用法小结_java

在java.lang包中有String.split()方法,返回是一个数组 我在应用中用到一些,给大家总结一下,仅供大家参考: 1.如果用"."作为分隔的话,必须是如下写法,String.split("\\."),这样才能正确的分隔开,不能用String.split("."); 2.如果用"|"作为分隔的话,必须是如下写法,String.split("\\|"),这样才能正确的分隔开,不能用String.s

string-才发现JAVA木有String.Split 方法

问题描述 才发现JAVA木有String.Split 方法 额,在网上搜索程序逻辑的代码,发现这么一个方法,就很疑惑的发现在JAVA中没有找到替代品... String.Split (String[], StringSplitOptions) 主要说根据String[]所包含的规则串来拆解String,而不是根据单一给定的字符. 嗯,想在JAVA中试运行的话,这个方法怎么搞呢? 附我现在想做的事情:制作一个"规则翻译器",可以根据输入好的规则将任意一篇文字翻译为程序单元,然后通过解析这

Java Tips:使用Pattern.split替代String.split

String.split方法很常用,用于切割字符串,split传入的参数是正则表达式,它的内部是每次都comiple正则表达式,再调用Pattern.split方法: public String[] split(String regex, int limit) {     return Pattern.compile(regex).split(this, limit);     }   public String[] split(String regex) {         return sp

Java String的常用方法及使用注意事项

一.Java String的常用方法: split()方法: equals()方法: substring()方法: 示例方法: private boolean isSameSelCode(Fbillconfirm fbillconfirm, HashMap outputParam){ String strExpenseID=new String(); Fbillconfirmdetail[] fbillconfirmdetail=fbillconfirm.getFbillconfirmdetai

Java中Split函数的用法技巧

在java.lang包中也有String.split()方法,与.net的类似,都是返回是一个字符型数组,但使用过程中还有一些小技巧.以下我就为大家介绍,需要的朋友可以参考下   如执行:"2|33|4".split("|") 出来的结果是: "" 2 3 3 4 奇怪吧,不过注意看一下API说明还是知道原因的. java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. strin

关于java 的split,求大神解决一下啊

问题描述 关于java 的split,求大神解决一下啊 代码 ",,,,".split(',') 我想得到Array('','','','') 但结果我却得到了Array() 有什么办法能得到我想要的结果呢... 解决方案 可以使用含有两个参数的split方法,如",,,,".split(",", -1),你调用的那个含有一个参数的方法默认实现如下: public String[] split(String regex) {return spli

关于Java String数组的问题希望大神解惑

问题描述 关于Java String数组的问题希望大神解惑 在一个抽象类类中有这么一句话protected String[] msg = null;public AbstractService(String msg) { this.msg = msg.split(Param.SPACE);//以空格为分隔符进行分割成数组 }没有为msg这个String数组赋值,然后有一个子类继承这个抽象类其中有这么几句话/** 登陆账号密码核对 */ private static final String SQ

Java的split方法使用详解_java

相信大家都经常使用String 的split方法,但是大家有没有遇到下面的这种情况: 大家想想下面的代码执行结果是什么 public static void main(String[] args) { // TODO Auto-generated method stub String str1 = "a,b,c,,,a"; String str2 = "a,b,c,,,"; String str3 = "a,b,c, , ,"; String[]

浅析Java中Split函数的用法技巧_java

如执行:"2|33|4".split("|")出来的结果是:""2 33 4奇怪吧,不过注意看一下API说明还是知道原因的. java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator,[limit]]) 参数 stringObj 必选项.要被分解的 String 对象或文字.该对象不会被 split 方法修改. separato