Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java字符串拆分为数组,如何将拆分后的字符保存到同一数组中?_Java_Android_Arrays_String - Fatal编程技术网

java字符串拆分为数组,如何将拆分后的字符保存到同一数组中?

java字符串拆分为数组,如何将拆分后的字符保存到同一数组中?,java,android,arrays,string,Java,Android,Arrays,String,例如:strEquation=“36+5-8X2/2.5” 我的代码是: String[] tmp = strEquation.split("[X\\+\\-\\/]+"); for(int i=0; i<tmp.length; i++) Log.d("Split array",tmp[i]); 我希望tmpstring数组也会放入我要拆分的字符,如下所示: tmp[0] = 36 tmp[1] = + tmp[2] = 5 tmp[3] = - tmp[4] = 8 tmp[

例如:
strEquation=“36+5-8X2/2.5”

我的代码是:

String[] tmp = strEquation.split("[X\\+\\-\\/]+");

for(int i=0; i<tmp.length; i++)
    Log.d("Split array",tmp[i]);
我希望
tmp
string数组也会放入我要拆分的字符,如下所示:

tmp[0] = 36
tmp[1] = +
tmp[2] = 5
tmp[3] = -
tmp[4] = 8
tmp[5] = X
tmp[6] = 2
tmp[7] = /
tmp[8] = 2.5

你知道怎么做吗?

在每个
X
+
-
/
字符之前或之后拆分怎么样?顺便说一句,您不必在字符类(
[…]
)中转义
+
/


String[]tmp=strEquation.split((?=[X+\\-/]))|(?我想说的是,您正在尝试获取所有匹配项,而不是如此分割字符串

Matcher m = Pattern.compile("[X+/-]|[^X+/-]+").matcher(strEquation);
while (m.find()) {
  System.out.println(m.group());
}
但上面的答案更聪明:)


另外:您不需要在方括号内转义
+
/
字符;只有当减号(
-
)不是列表中的第一个或最后一个字符时,才需要转义它

检查这些链接和注释。对于双引号中的所有复杂正则表达式,您可以只使用“//D”,
String[] tmp = strEquation.split("(?=[X+\\-/])|(?<=[X+\\-/])");
Matcher m = Pattern.compile("[X+/-]|[^X+/-]+").matcher(strEquation);
while (m.find()) {
  System.out.println(m.group());
}