I'm getting dynamic data from Database,that is in format of /name/exam/0oecda251d73jf82m33m92/run.
But now I want only 0oecda251d73jf82m33m92 how to get this in Java. I tried with stringtokenizer class but not get correct result.
I would use a regular expression and a Pattern. Something like,
String str = "/name/exam/0oecda251d73jf82m33m92/run";
Pattern p = Pattern.compile("/.*/.*/(.*)/run");
Matcher m = p.matcher(str);
if (m.matches()) {
System.out.println(m.group(1));
}
or use String.split(String); the StringTokenizer Javadoc says (in part) StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Something like,
String[] arr = str.split("/");
if (arr.length > 3) {
System.out.println(arr[3]);
}
Both of which output
0oecda251d73jf82m33m92
Use split function of String class.
String s[] = "/name/exam/0oecda251d73jf82m33m92/run".split("/");
System.out.println(s[3]);
As the above answer said it is not recommended to use StringTokenizer in new code if you want to work with split.