This question already has answers here:
substring index range
(9 answers)
Finding substring in RegEx Java
(5 answers)
Closed 4 years ago.
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 .
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
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);
Related
This question already has answers here:
How can I remove a substring from a given String?
(14 answers)
Closed 2 years ago.
String string = "yesnoyesnoyesnoyesno";
String substring = "no";
How do I remove every occurrence of the substring from the string so it ends up being "yesyesyesyes"?
If you are using Java 8+, then you could try splitting the input on "no" and then joining the resulting array together into a string:
String string = "yesnoyesnoyesnoyesno";
string = String.join("", string.split("no"));
System.out.println(string);
This prints:
yesyesyesyes
This doesn't use the replace(), so technically it meets the brief:
String str = string.replaceAll("no", "");
String input = "yesnoyesnoyesno";
String output="";
for(String token : input.split("no") )
output = output.concat(token);
System.out.println(output);
It prints:
yesyesyesyes
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a string named extractDb values can be
GDSHKG.db
GDSMNH.db
GDSTKY.db
GDSLDN.db
now i want to remove the .extension partof it
that is i want to see the result as
GDSHKG
GDSMNH
GDSTKY
GDSLDN
please advise how to achieve this in java
Try to use substring like this:
String myString = "GDSHKG.db";
String newString = myString.substring(0, myString.indexOf("."));
System.out.print(newString);
Output:
GDSHKG
This question already has answers here:
String.split(".") is not splitting my long String
(2 answers)
Closed 8 years ago.
My problem is that double value converted to string cannot be splitted by dot.
Here you can see my code:
String valueOf = String.valueOf(12.34);
System.out.println("valueOf=" + valueOf);
String[] split = valueOf.split(".");
System.out.println("split=" + Arrays.toString(split));
The output is:
valueOf = 12.34
split = []
Why is split array empty?
You can try to run it on https://ideone.com/BBL4z2.
You need to escape . here. you can use \\.
String[] split = valueOf.split("\\.");
Because in regex you need to escape .
This question already has an answer here:
Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token) [duplicate]
(1 answer)
Closed 8 years ago.
I have a String(freeText) "Manas \"Jaikant IBM\"". I want to split into two strings:
String normalMatch="Manas";
String exactMatch="Jaikant IBM";
It means that String normalMatch contains Manas and String exactMatch contains Jaikant IBM.
i am using the split() method of String class in Java
String[] splittedText= freeText.split("\\s");
I am getting 3 string elements but i need 2 string elements only.
Use substring instead of split:
int index = freeText.indexOf(" ");
String normalMatch = freeText.substring(0,index);
String exactMatch = freeText.substring(index); // endIndex == freeText.length())
Split on quotes(") then, you will get Manas and Jaikant IBM and can ignore the 3rd value.
This question already has answers here:
String.Split with a String?
(2 answers)
Closed 9 years ago.
I have a string "552 s hello"
I want to get the "552" in one substring and "s" in other substring and I want to split this String on the basis of first two spaces.Please help...
You can do it like this:
String t = "522 s hello"
String[] test = t.split(" ");
Now in the String array you will have three values, "522", "s" and "hello. And you can now access each.
Use String#split(String regex):
String[] sp="552 s hello".split(" ");
String fiveFiveTwo=sp[0];
String letterS=sp[1];
The names aren't very useful in the end, so you'll want to rename the strings I've created. You should also catch ArrayIndexOutOfBoundsException.