[英]Splitting a string at a comma in java
I have a string with an unknown length that looks like this 我有一个长度未知的字符串,看起来像这样
a ,b, c: integer; d,e :real;
How could I split this at the commas without getting the words and characters that follow the last commas of the variables? 在没有逗号和变量最后一个逗号后面的字符的情况下,如何在逗号处拆分呢? Or is there a way to split the variables at the commas after it is in an array from splitting at the colon?
还是有一种方法可以将逗号分隔的数组中的变量从冒号处拆分出来? The closest I have come is with this method
我最接近的就是这种方法
// the variablesArray would contain a ,b, c in the first index
// and d,e in the second index.
public static String[] variables(String[]variablesArray) {
String variables[] = new String[variablesArray.length];
for(int i = 0; i < variablesArray.length; i++){
String temporary = variablesArray[i];
String arr[] = temporary.split(",");
}
return variables;
}
When I run this it only returns the first character in the array index 当我运行它时,它仅返回数组索引中的第一个字符
//prints [ad]
You'll need to break the problem up: 您需要解决问题:
;
;
. :
:
分割字符串:
Note that this will give you the array {"a ,b, c", "d,e"}
as I understand from your question that you want it. 请注意,据我从您的问题中了解到,这将为您提供数组
{"a ,b, c", "d,e"}
。 If you want to split that further, you'll need to split each of these strings again , this time at ,
. 如果您想进一步分裂,则需要再次拆分这些字符串,这个时间
,
。 Note that you'll need to change the type of your array from String[]
to String[][]
in this case. 请注意,在这种情况下,您需要将数组的类型从
String[]
更改为String[][]
。 You'll get {{"a", "b", "c"}, {"d", "e"}}
. 您将得到
{{"a", "b", "c"}, {"d", "e"}}
。
If you also want to strip whitespace during the process of splitting, add \\s*
before and after the split character. 如果您还想在拆分过程中去除空格,请在拆分字符前后添加
\\s*
。 For example: "a, b,c , d".split("\\\\s*,\\\\s*")
returns {"a", "b", "c", "d"}
. 例如:
"a, b,c , d".split("\\\\s*,\\\\s*")
返回{"a", "b", "c", "d"}
。
if myString
is equal to "a ,b, c: integer; d,e :real;"
如果
myString
等于"a ,b, c: integer; d,e :real;"
the following with output abcde
以下与输出
abcde
for (String type : myString.split(";")) {
String sub = type.substring(0, type.indexOf(":"));
for (String name : sub.split(",")) {
System.out.println(name);
}
}
I think you have a problem. 我觉得你有问题。
temporary.split(",");
is an array then you have appened it to be as a part of another array. 是一个数组,那么您已经将其附加为另一个数组的一部分。 Thats troublesome .... I mean:
那很麻烦..我的意思是:
Array
0 => Array
0 => a
1 => b
So you should use: 因此,您应该使用:
String arr = temporary.split(",");
how about following? 怎么样?
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
String str = "a ,b,c: integer;d,e :real;";
String[] stra = str.split(";");
for(String st: stra){
//System.out.println(st);
String[] stra2 = st.split(",");
for(String s: stra2){
System.out.println(s);
list.add(String.valueOf(s.charAt(0)));
}
}
System.out.println(list);
}
OP OP
a
b
c: integer
d
e :real
[a, b, c, d, e]
is this what you want? 这是你想要的吗?
String[] result = s.replaceAll("\\w{2,}", "").replaceAll("[:;,]"," ").replaceAll("\\u0020+"," ").split(" ");
Array result
now holds only the single chars. 数组
result
现在仅包含单个字符。 Testing with: 测试:
for(String c: array)
System.out.println(c);
will output: 将输出:
a
b
c
d
e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.