我现在有一个像abc-5,xyz-9,pqr-15这样的字符串,我只想在"-“之后获得值,那么,我如何才能获得value..and,我想在字符串数组中获得这个值?
发布于 2012-12-03 20:26:48
int pos = string.indexOf('-');
String sub = string.substring(pos);如果每个字符串中有多个值,则必须首先拆分它(使用split方法)。例如:
String[] array = string.split(',');
String[] values = new String[array.length];
for(int i = 0; i < array.length; i++)
values[i] = array[i].substring(arrays[i].indexOf('-'));现在,您拥有了数组中的值。
发布于 2012-12-03 20:27:01
你可以试试
String string = "abc-5,xyz-9,pqr-15";
String[] parts = string.split(",");
String val1 = parts[0].split("-");
.....诸若此类
发布于 2012-12-03 20:28:03
我会在你的字符串上使用split。
String str = "abc-5,xyz-9,pqr-15";
String[] arr = str.split(",");
for (String elem: arr) {
System.out.print(elem.split("-")[1] + " : "); // Will print - `5 : 9 : 15`
}或者像这样使用Regular Expression:-
Matcher matcher = Pattern.compile("-(\\d+)").matcher(str);
while(matcher.find()) {
System.out.println(matcher.group(1));
}https://stackoverflow.com/questions/13683207
复制相似问题