If you want only the 12th value
Using split
: (very simply, a bit inefficient, but probably not too bad – I wouldn’t bother with anything else unless I’m writing production code)
System.out.println(str.split(",")[12]);
Using indexOf
: (somewhat more complex, way more efficient)
int index = 0;
for (int i = 0; i < 12; i++)
index = str.indexOf(',', index) + 1;
System.out.println(str.substring(index, str.indexOf(',', index)));
Using regex: (probably more complicated than it’s worth)
Pattern pattern = Pattern.compile("^(?:[^,]*,){12}([^,]*)");
Matcher matcher = pattern.matcher(str);
while (matcher.find())
System.out.println(matcher.group(1));
If you want everything from the 12th value
Using indexOf
:
int index = 0;
for (int i = 0; i < 12; i++)
index = str.indexOf(',', index) + 1;
System.out.println(str.substring(index));
Using regex:
Pattern pattern = Pattern.compile("^(?:[^,]*,){12}(.*)");
Matcher matcher = pattern.matcher(str);
while (matcher.find())
System.out.println(matcher.group(1));
Test.
Check out this page for more on Java regular expressions.
solved java regular expression take next value [closed]