0

I would like to extract a substring starting from particular substring.

I'm getting an array of URIs of multiple images from Photo Library via this solution. But the URIs are something like this

content://com.android.providers.media.documents/document/image%3A38

I would like to remove content:// and get only

com.android.providers.media.documents/document/image%3A38

I've searched through the Internet but found no best solution. Perhaps to avoid regex because it's kinda heavy.

At the moment I choose not to get the substring by checking after second '/' because it feels kinda "hardcoded".

Not sure if I've missed a good solution but please help.

Paras Korat
  • 1,888
  • 2
  • 16
  • 35
felixwcf
  • 2,040
  • 26
  • 43

2 Answers2

3

If you need to get whatever string comes after a certain substring, in this case "content://", you could use the split method.

String string = "content://com.android.providers.media.documents/document/image%3A38";
String uri = string.split("content://")[1];

Or you could use the substring and indexOf methods like in the other answer, but add on the length of the substring.

String string = "content://com.android.providers.media.documents/document/image%3A38";
String sub = "content://";
String uri = string.substring(string.indexOf(sub) + sub.length());
1

You can just use the substring method in order to create new strings without content://, something like this :

String string = "content://com.android.providers.media.documents/document/image%3A38"
String secondString = string.substring(string.indexOf("com.android"));
Guy Luz
  • 2,714
  • 14
  • 35
Tamir Abutbul
  • 6,886
  • 7
  • 24
  • 48