I have a string that has the value of name:score
. I want to split the string into two strings, string a
with the value of name
and string b
with the value of score
.
我有一个值为name的字符串:score。我想把字符串分割成两个字符串,字符串a的值是name,字符串b的值是score。
What is the correct function/syntax to do this?
正确的函数/语法是什么?
I have looked at string.split
, but can not find the actual syntax to return the data into two separate strings.
我看过弦。拆分,但无法找到将数据返回到两个独立字符串的实际语法。
7 个解决方案
#1
31
The split
function is suitable for that :
分割函数适用于:
String[] str_array = "name:score".split(":");
String stringa = str_array[0];
String stringb = str_array[1];
#2
6
You need to look into Regular Expressions:
你需要观察正则表达式:
String[] s = myString.split("\\:"); // escape the colon just in case as it has special meaning in a regex
Or you can also use a StringTokenizer.
或者你也可以使用StringTokenizer。
#3
2
Use:
使用:
String [] stringParts = myString.split(":");
#4
2
String row = "name:12345";
String[] columns = row.split(":");
assert columns.length == 2;
String name = columns[0];
int score = Integer.parseInt(columns[1]);
#5
1
Split creates an array with your strings in it:
Split创建一个包含您的字符串的数组:
String input = "name:score";
final String[] splitStringArray = input.split(":");
String a = splitStringArray[0];
String b = splitStringArray[1];
#6
1
$ cat Split.java
猫Split.java美元
public class Split {
public static void main(String argv[]) {
String s = "a:b";
String res[] = s.split(":");
System.out.println(res.length);
for (int i = 0; i < res.length; i++)
System.out.println(res[i]);
}
}
$ java Split
美元java分裂
2
a
b
#7
1
what if you have something like this a:1:2 name = a:1??
如果你有像a:1:2的名字= a:1?
private String extractName(String str) {
String[] split = str.split(":");
return str.replace(split[split.length - 1], "");
}
private int extractId(String str){
String[] split = str.split(":");
return Integer.parseInt(CharMatcher.DIGIT.retainFrom(split[split.length-1]));
}
#1
31
The split
function is suitable for that :
分割函数适用于:
String[] str_array = "name:score".split(":");
String stringa = str_array[0];
String stringb = str_array[1];
#2
6
You need to look into Regular Expressions:
你需要观察正则表达式:
String[] s = myString.split("\\:"); // escape the colon just in case as it has special meaning in a regex
Or you can also use a StringTokenizer.
或者你也可以使用StringTokenizer。
#3
2
Use:
使用:
String [] stringParts = myString.split(":");
#4
2
String row = "name:12345";
String[] columns = row.split(":");
assert columns.length == 2;
String name = columns[0];
int score = Integer.parseInt(columns[1]);
#5
1
Split creates an array with your strings in it:
Split创建一个包含您的字符串的数组:
String input = "name:score";
final String[] splitStringArray = input.split(":");
String a = splitStringArray[0];
String b = splitStringArray[1];
#6
1
$ cat Split.java
猫Split.java美元
public class Split {
public static void main(String argv[]) {
String s = "a:b";
String res[] = s.split(":");
System.out.println(res.length);
for (int i = 0; i < res.length; i++)
System.out.println(res[i]);
}
}
$ java Split
美元java分裂
2
a
b
#7
1
what if you have something like this a:1:2 name = a:1??
如果你有像a:1:2的名字= a:1?
private String extractName(String str) {
String[] split = str.split(":");
return str.replace(split[split.length - 1], "");
}
private int extractId(String str){
String[] split = str.split(":");
return Integer.parseInt(CharMatcher.DIGIT.retainFrom(split[split.length-1]));
}