[英]split() method on string not working
i want to split a string which contains +, im using split() on String object as: but it shows exception. 我想分割一个包含+的字符串,即时消息在String对象上使用split()为:但它显示异常。
class StringTest
{
public static void main(String[] args)
{
String val= "004+0345564";
String arr[]=val.split("+");
for(int i=0;i<=arr.length-1;i++){
System.out.println(arr[i]);
}
}
}
String.split
takes a regular expression as its argument. String.split
将正则表达式作为其参数。 A +
in a regular expression has a special meaning. 正则表达式中的
+
具有特殊含义。 (One or more of previous). (以前的一个或多个)。
If you want a literal +
, you need to escape it using \\\\+
. 如果要使用文字
+
,则需要使用\\\\+
对其进行转义 。 (The regular expression grammar is \\+
but you need to escape the backslash itself in Java using a second backslash). (正则表达式语法为
\\+
但是您需要在Java中使用第二个反斜杠对反斜杠本身进行转义)。
String arr[] = val.split("\\+");
代替
String arr[]=val.split("+");
Split takes regex. 拆分使用正则表达式。 You need to escape
+
您需要逃跑
+
String arr[]=val.split("\\+")
String arr[] = val.split("\\+");
try this 尝试这个
class StringTest
{
public static void main(String[] args)
{
String val= "004+0345564";
String arr[]=val.split("\\+");
for(int i=0;i<=arr.length-1;i++){
System.out.println(arr[i]);
}
}
}
You need to use 您需要使用
String arr[] = val.split("\\+");
Instead of 代替
String arr[]=val.split("+");
The split method takes regex as inputs. split方法将正则表达式作为输入。 You can also refer String#split to confirm the same.
您也可以引用String#split进行确认。
Actual syntax is 实际语法是
public String[] split(String regex, int limit)
//or
public String[] split(String regex)
So use, below one. 因此,使用下面的一个。
String arr[] = val.split("\\+",0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.