1

I would like to extract ccadmin from /ccadmin/hrp/filelist ?

i know that i can get last substring using

String uri = "/ccadmin/hrp/filelist";
String commandKey = uri.substring(uri.lastIndexOf("/") + 1, uri.length()); 

but how to extract first Substring ccadmin?

ccheneson
  • 49,072
  • 8
  • 63
  • 68
SoftEngineer1
  • 57
  • 1
  • 10
  • We need more details. What are the assumptions? Can we assume the string you are parsing will always be the same format? – Jonathan Henson Sep 05 '14 at 15:48
  • Also a possible duplicate of [Java: Getting a substring from a string starting after a particular character](http://stackoverflow.com/q/14316487/3224483), which is actually in Java. – Rainbolt Sep 05 '14 at 15:51

3 Answers3

7

If I understand your question, then you could use something like,

String uri = "/ccadmin/hrp/filelist";
String first = uri.substring(1, uri.indexOf("/", 2));
System.out.println(first);

Output is

ccadmin
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
3

Split the string by the / character:

String[] parts = uri.split("/");

parts[1] is what you want.

RockOnRockOut
  • 751
  • 7
  • 18
0

you can use this to split your string and then get the part of uri you need:

String uri = "/s/d/f";
        String[] parts = uri.split("/");
        String item = parts[1];
        System.out.println( item );
Anton Selin
  • 3,462
  • 6
  • 19
  • 23