I have a String name packageName="https://play.google.com/store/apps/details?id=com.MoversGames.DinosaurWorldGame";
How i can get subString
from this string. I want to take "id=com.MoversGames.DinosaurWorldGame"
this from String packgeName
. The length of this String may change. It is not constant its variable .
Asked
Active
Viewed 3,891 times
0

AskNilesh
- 67,701
- 16
- 123
- 163

Adnan Ijaz
- 27
- 1
- 8
-
you can do it using regex – Nawnit Sen Oct 11 '18 at 10:50
-
@NawnitSen Regex? You could also do it with a 3rd party webserver, but why would you need so powerful for something so simple. – Nick Cardoso Oct 11 '18 at 10:51
-
1Just split the String with the delimiter `?` and you will get your result. – Kunu Oct 11 '18 at 10:53
-
@NickCardoso that is the first thing that came to my mind. – Nawnit Sen Oct 11 '18 at 10:53
2 Answers
2
You need to use String.substring()
Try to substring your string from the index of id
to length
of your string
In this below example i have started substring from index of ?
+1 so it will start substring
your string from id
to length of your string
SAMPLE CODE
String packageName = "https://play.google.com/store/apps/details?id=com.MoversGames.DinosaurWorldGame";
String answer = packageName.substring(packageName.indexOf("?")+1, packageName.length() );
Log.e("ANSWER", answer);
OUTPUT
2018-10-11neel.com.myapplication E/ANSWER: id=com.MoversGames.DinosaurWorldGame

AskNilesh
- 67,701
- 16
- 123
- 163
0
There might be also some more query parameters in your String , so you can also try it like this :
String packageName = "https://play.google.com/store/apps/details?i
id=com.MoversGames.DinosaurWorldGame&x=y&z=a";
String answer = packageName.substring(packageName.indexOf("id"));
System.out.print("ANSWER", answer);

rootExplorr
- 575
- 3
- 17