创建其他字符串(Java)的
问题描述:
我想从一个给定的字符串中提取数据的新字符串。 例子:创建其他字符串(Java)的
我有类似:
String s = "tree;dog;house;computer sience";
而且我要为结果:
String a = "tree";
String b = "dog";
String c = "house";
String d = "computer sience";
子串具有可变长度,但被分隔 “;”。任何想法如何做到这一点?
答
试试这个方法:
String inpu="tree;dog;house;computer sience";
String[] sArray = s.split(";");
for (String ss: inputString) {
System.out.print(ss+"\n");
}
输出:
tree
dog
house
computer sience
+0
感谢,伟大工程! –
+0
@Lukas W欢迎 –
答
String s="tree;dog;house;computer sience";
String[] strings = s.split(";");
String a = strings[0];
String b = strings[1];
String c = strings[2];
String d = strings[3];
//test with:
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
答
String s="tree;dog;house;computer sience";
Scanner scanner = new Scanner(s).useDelimiter(";");
String a = scanner.next();
String b = scanner.next();
String c = scanner.next();
String d = scanner.next();
scanner.close();
你总是在 'S' 4个变量? – khriskooper
是的,总是4个变量 –
@Lukas W.看一看并批准的答案,如果它成为你的目的 –