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.

2 Answers 2

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

Comments

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"));

Comments

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.