String splitting [duplicate] - java

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
Say I have a string a such:
String str = "Kellogs Conflakes_$1.20";
How do I get the preceding values before the dollar ($) sign.
N.B: The prices could be varied say $1200.

You can return the substring using substring and the index of the $ character.
str = str.substring(0, str.indexOf('$'));

You could use String.split(String s) which creates a String[].
String str = "Kellogs Conflakes_$1.20"; //Kellogs Conflakes_$1.20
String beforeDollarSign = String.split("$").get(0); //Kellogs Conflakes_
This will split the String str into a String[], and then gets the first element of that array.

Just do this for split str.split("$") and store it on an array of String.
String[] split = str.split("$");
And then get the first position of the array to get the values that you have before the $
System.out.println(split[0]); //Kellogs Conflakes_
At the position 1 you will have the rest of the line:
System.out.println(split[1]); //1.20

Try this:
public static void main(String[] args)
{
String str = "Kellogs Conflakes_$1.20";
String[] abc=str.split("\ \$");
for(String i:abc)
{
System.out.println(i);
}
}
after this you can easily get abc[0]

Related

How to remove a substring from a string without using replace() method? [duplicate]

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

Convert String in Array form to String Array [duplicate]

This question already has answers here:
Reverse (parse the output) of Arrays.toString(int[]) [duplicate]
(3 answers)
Closed 6 years ago.
Let's say I've got a string with three strings looking like this:
String s = "[object1,object2,object3]";
How do I convert this to a String array which looks like this:
String[] ary = {"object1", "object2", "object3"};
Also mind that each string item (object1, object2 and object3) may also contain additional comma (,) characters.
just use the split method String[] str = s.split(",");
if you want the "[ ]" out use the string.replace method to replace "[]" with ""
I assume you want don't want the outer [], you could use replace/split or substring/split
String myArray[] = s.replace("[","").replace("]","").split(",");
Or
String myArray[] = s.substring(1, s.length() -1 ).split(",");
See the code below with comments to explain what each does.
s = s.replace("[","").replace("]},""); //remove the brackets
String[] ary = s.split(","); //split the string around commas
To do the stuff in your situation you can do
String[] ary = s.substring(1, s.length() -1).split(",");
If there is no "[" and "]" just remove the substring call

How to Split String With double quotes in java [duplicate]

This question already has an answer here:
Divide/split a string on quotation marks
(1 answer)
Closed 8 years ago.
This question is Pretty Simple
How to Split String With double quotes in java?,
For example I am having string Do this at "2014-09-16 05:40:00.0",After Splitting, I want String like
Do this at
2014-09-16 05:40:00.0,
Any help how to achieve this?
This way you can escape inner double quotes.
String str = "Do this at \"2014-09-16 05:40:00.0\"";
String []splitterString=str.split("\"");
for (String s : splitterString) {
System.out.println(s);
}
Output
Do this at
2014-09-16 05:40:00.0
Use method String.split()
It returns an array of String, splitted by the character you specified.
public static void main(String[] args) {
String test = "Do this at \"2014-09-16 05:40:00.0\"";
String parts[] = test.split("\"");
String part0 = parts[0];
String part1 = parts[1];
System.out.println(part0);
System.out.println(part1);
}
output
Do this at
2014-09-16 05:40:00.0
Try this code. Maybe it can help
String str = "\"2014-09-16 05:40:00.0\"";
String[] splitted = str.split("\"");
System.out.println(splitted[1]);
The solutions provided thus far simply split the string based on any occurrence of double-quotes in the string. I offer a more advanced regex-based solution that splits only on the first double-quote that precedes a string of characters contained in double quotes:
String[] splitStrings =
"Do this at \"2014-09-16 05:40:00.0\"".split("(?=\"[^\"].*\")");
After this call, split[0] contains "Do this at " and split[1] contains "\"2014-09-16 05:40:00.0\"". I know you don't want the quotes around the second string, but they're easy to remove using substring.

RegEx for splitting the string by Space but ignoring spaces in quotes [duplicate]

This question already has answers here:
Regex for splitting a string using space when not surrounded by single or double quotes
(16 answers)
Closed 8 years ago.
I have a string,
String string = "sdeLING9497896,\"kifssd9497777 999_13\",Kfsheis9497896e,ersdG9497896,aseLING9497896 erunk15426 \nEsea4521
\"\nSdfes451 45264\" \"kiseliog949775 959_13\"";
I may have spaces, comma, tab or new line character in this string. My requirement is to split this string using space,comma,tab and new line but the spaces inside the double quoted strings should be excluded.
My sample code:
public class RegExTest {
public static void main(String[] args) {
Set<String> idSet = new HashSet<String>();
String string = "sdeLING9497896,\"kipliog9497777 999_13\",KIPLING9497896e,ersdG9497896,aseLING9497896 erunk15426 \nEsea4521 \"\nSdfes451 45264\"";
String ids[] = string.split(**"NEED A REGEX HERE"**);
for (String id : ids) {
if (id.trim().length() > 0) {
idSet.add(id);
}
}
System.out.println(idSet);
}
}
My expectation is: sdeLING9497896, kifssd9497777 999_13, KIPLING9497896e, ersdG9497896, aseLING9497896, erunk15426, Esea4521, Sdfes451 45264, kiseliog949775 959_13
Please guide me to solve this!
You can use this regex:
String[] tok = string.split("(?=((\\S*\\s){2})*\\S*$)[\\s,]+");
This will split on \s or , only if \s is outside double quotes by making sure there are even number of quotes after delimiter \s.

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.

Categories

Resources