Sub string from the String [duplicate] - java

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.

Related

Getting SubString from String in Java(Android) [duplicate]

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

Split the string in java with change in alphabets and in groups [duplicate]

This question already has an answer here:
Split regex to extract Strings of contiguous characters
(1 answer)
Closed 5 years ago.
How to split a string in java with change in alphabetical order.
For example split aaaabb to aaaa & bb.
I tried to sort the string by sort function but i am unable to split alphabets in groups.
Try with,
String str = "aaaaabbb";
String strArray[] = str.split("(?<=(.))(?!\\1)");
for(String value : strArray){
System.out.println(value);
}

Splitting a word where "." occurs [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 7 years ago.
I need to be able to split a single word string like "x.y" into an array of size 2 where the "." occurs, so the array would look like ["x", "y"]. What I've tried is:
String word = "hello.world";
String[] split = word.split(".")
return split.length == 2;
but this seems to just return an empty array (false). How would I go about doing this? Thanks.
Repalce this
String[] split = word.split(".")
with
String[] split = word.split("\\.")
. (dot) has a special meaning in regex so you need to escape it if you want to use it as a literal to split.

Splitting a String using split() not getting required result [duplicate]

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.

Java split function [duplicate]

This question already has answers here:
split string on comma, but not commas inside parenthesis
(3 answers)
Closed 9 years ago.
Hi I have a question about java split
ItemName,Item,,(width,length,height),12
I want String[] equals this"
String[] result ={"ItemName","Item","","(width,length,height)","12"};
Who can help me with using split function or other regex?
If your input string is named istr, then
String[] result = istr.split(",");
should work.
try this
String data="ItemName,Item,,(width,length,height),12";
String[] result=data.split("//,");
for(String string : result) {
System.out.println(string);
}

Categories

Resources