0

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.

0

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

split is working fine.Thank you for your response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.