-2

I want to display the first folder name from the path. /mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java

/mnt/sdcard/Videos/lk.jpeg--> **mnt**
demongolem
  • 9,474
  • 36
  • 90
  • 105
user3243931
  • 29
  • 1
  • 6

3 Answers3

2

You can split on / and use [1] element from result array.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).

String desired = "/mnt/sdcard/foo".split("/")[1];
Danstahr
  • 4,190
  • 22
  • 38
1
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);

Try this out. This is a poor question. But maybe the asker can be a newbie.

Dinal
  • 661
  • 4
  • 9
  • Are you using the exact same code I pasted here. There would be five elements in the array...Then how will it throw ArrayIndexOutOfBoundsException. If you are using some other string then enclose the statements with this condition. if(str.split("/").length > 1) – Dinal Jun 03 '14 at 11:42