I am trying to split a string like the string below
我正在尝试拆分字符串,如下面的字符串
3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4
to a table of strings which does not have any number or sign.
到一个没有任何数字或符号的字符串表。
That means
a[0]=x
a[1]=y
a[2]=x
a[3]=w
I tried this
我试过这个
split("(\\+|\\-|\\d)+\\d*")
but it seems that it does not work.
但似乎它不起作用。
6 个解决方案
#1
5
The following should work:
以下应该有效:
String[] letters = input.split("[-+\\d]+");
#2
3
Edit: -
If you want xw
to be together in your resulting array, then you would need to split your string: -
如果你希望xw在你的结果数组中在一起,那么你需要拆分你的字符串: -
String[] arr = str.split("[-+\\d]+");
Output: -
[, x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]
You can replace all the unwanted characters with empty string, and split on empty string.
您可以用空字符串替换所有不需要的字符,并在空字符串上拆分。
String str = "3x2y3+5x2w3-8x2w3z4+3-2x2w3+9y-4xw-x2x3+8x2w3z4-4";
str = str.replaceAll("[-+\\d]", "");
String[] arr = str.split("");
System.out.println(Arrays.toString(arr));
Note that, this will add an empty string as the first element of your array, which you can handle.
请注意,这将添加一个空字符串作为数组的第一个元素,您可以处理它。
Output: -
[, x, y, x, w, x, w, z, x, w, y, x, w, x, x, x, w, z]
Note that -
sign in your question is different. You should replace it with the one on your keyboard. Currently it is not matching -
sign.
请注意 - 登录您的问题是不同的。您应该用键盘上的那个替换它。目前它不匹配 - 标志。
#3
1
This one-liner does it all:
这个单行可以做到这一切:
String[] letters = input.replaceAll("(^[^a-z]*)|([^a-z]*$)", "").split("[^a-z]+");
This also deals with leading/trailing characters so you don't get blank elements at the start of the array (like some other answers)
这也处理前导/尾随字符,因此您不会在数组的开头获得空白元素(就像其他一些答案)
A test with your string:
用你的字符串测试:
public static void main(String[] args) {
String input = "3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
String[] letters = input.replaceAll("(^[^a-z]*)|([^a-z]*$)", "").split("[^a-z]+");
System.out.println(Arrays.toString(letters));
}
Output:
[x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]
Notice that there's no leading "blank" element in the array
请注意,数组中没有前导“空白”元素
#4
0
String[] letters = input.split("[\\d\\+\\-]+");
#5
0
Is this what you are trying to achieve?
这是你想要实现的目标吗?
String data="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
//lets replace all unnecessary elements with spaces
data=data.replaceAll("[-+–\\d]", " ");
// now string looks like:
// " x y x w x w z x w y xw x x x w z "
// lets remove spaces from start and end
data=data.trim();
// data looks like:
// "x y x w x w z x w y xw x x x w z"
// and split in places where is at least one space
String[] arr=data.split("\\s+");
System.out.println(Arrays.toString(arr));
Output:
[x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]
#6
0
remark - and – are not same code, one is just ascii minus other is long ( coded UTF8 e28093 )
注释 - 和 - 不是相同的代码,一个是ascii减去其他很长(编码UTF8 e28093)
public class Test {
public static void main(String pArgs[])
{
String s="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
String splitreg="(\\+|\\-|\\d|–)+\\d*"; if ( pArgs.length > 0 )
{
splitreg=pArgs[0];
}
System.out.println("splitting '" + s + "' with '" + splitreg + "'");
String[] splitted=s.split(splitreg);
for (int i=0; i < splitted.length; i++ )
{
System.out.println("["+ i + "]" + "=" + splitted[i]);
}
}
}
/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java Test
splitting '3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4' with '(\+|\-|\d|–)+\d*'
[0]=
[1]=x
[2]=y
[3]=x
[4]=w
[5]=x
[6]=w
[7]=z
[8]=x
[9]=w
[10]=y
[11]=xw
[12]=x
[13]=x
[14]=x
[15]=w
[16]=z
#1
5
The following should work:
以下应该有效:
String[] letters = input.split("[-+\\d]+");
#2
3
Edit: -
If you want xw
to be together in your resulting array, then you would need to split your string: -
如果你希望xw在你的结果数组中在一起,那么你需要拆分你的字符串: -
String[] arr = str.split("[-+\\d]+");
Output: -
[, x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]
You can replace all the unwanted characters with empty string, and split on empty string.
您可以用空字符串替换所有不需要的字符,并在空字符串上拆分。
String str = "3x2y3+5x2w3-8x2w3z4+3-2x2w3+9y-4xw-x2x3+8x2w3z4-4";
str = str.replaceAll("[-+\\d]", "");
String[] arr = str.split("");
System.out.println(Arrays.toString(arr));
Note that, this will add an empty string as the first element of your array, which you can handle.
请注意,这将添加一个空字符串作为数组的第一个元素,您可以处理它。
Output: -
[, x, y, x, w, x, w, z, x, w, y, x, w, x, x, x, w, z]
Note that -
sign in your question is different. You should replace it with the one on your keyboard. Currently it is not matching -
sign.
请注意 - 登录您的问题是不同的。您应该用键盘上的那个替换它。目前它不匹配 - 标志。
#3
1
This one-liner does it all:
这个单行可以做到这一切:
String[] letters = input.replaceAll("(^[^a-z]*)|([^a-z]*$)", "").split("[^a-z]+");
This also deals with leading/trailing characters so you don't get blank elements at the start of the array (like some other answers)
这也处理前导/尾随字符,因此您不会在数组的开头获得空白元素(就像其他一些答案)
A test with your string:
用你的字符串测试:
public static void main(String[] args) {
String input = "3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
String[] letters = input.replaceAll("(^[^a-z]*)|([^a-z]*$)", "").split("[^a-z]+");
System.out.println(Arrays.toString(letters));
}
Output:
[x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]
Notice that there's no leading "blank" element in the array
请注意,数组中没有前导“空白”元素
#4
0
String[] letters = input.split("[\\d\\+\\-]+");
#5
0
Is this what you are trying to achieve?
这是你想要实现的目标吗?
String data="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
//lets replace all unnecessary elements with spaces
data=data.replaceAll("[-+–\\d]", " ");
// now string looks like:
// " x y x w x w z x w y xw x x x w z "
// lets remove spaces from start and end
data=data.trim();
// data looks like:
// "x y x w x w z x w y xw x x x w z"
// and split in places where is at least one space
String[] arr=data.split("\\s+");
System.out.println(Arrays.toString(arr));
Output:
[x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]
#6
0
remark - and – are not same code, one is just ascii minus other is long ( coded UTF8 e28093 )
注释 - 和 - 不是相同的代码,一个是ascii减去其他很长(编码UTF8 e28093)
public class Test {
public static void main(String pArgs[])
{
String s="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
String splitreg="(\\+|\\-|\\d|–)+\\d*"; if ( pArgs.length > 0 )
{
splitreg=pArgs[0];
}
System.out.println("splitting '" + s + "' with '" + splitreg + "'");
String[] splitted=s.split(splitreg);
for (int i=0; i < splitted.length; i++ )
{
System.out.println("["+ i + "]" + "=" + splitted[i]);
}
}
}
/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java Test
splitting '3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4' with '(\+|\-|\d|–)+\d*'
[0]=
[1]=x
[2]=y
[3]=x
[4]=w
[5]=x
[6]=w
[7]=z
[8]=x
[9]=w
[10]=y
[11]=xw
[12]=x
[13]=x
[14]=x
[15]=w
[16]=z