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.
Related
This question already has answers here:
Java - How to split a string on plus signs?
(2 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Just found out, that I get a NullPointerException when trying to split a String around +, but if I split around - or anything else (and change the String as well of course), it works just fine.
String string = "Strg+Q";
String[] parts = string.split("+");
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Q
Would love to hear from you guys, what I am doing wrong!
This one works:
String string = "Strg-Q";
String[] parts = string.split("-");
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Q
As + is one of the special regex syntaxes you need to escape it.
Use
String[] parts = string.split("\\+");
Instead of
String[] parts = string.split("+");
Try this:
final String string = "Strg+Q";
final String[] parts = string.split("\\+");
System.out.println(parts[0]); // Strg
System.out.println(parts[1]); // Q
It happens because + is a special character in Regex - it's the match-one-or-more quantifier.
The only thing you need to do is to escape it:
String[] parts = string.split("\\+");
String.split(...) expects a Regular Expression and the '+' is a special character. Try:
String string = "Strg+Q";
String[] parts = string.split("[+]");
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Q
+ is a special regex character which means that you need to escape it in order to use it as a normal character.
String[] parts = string.split("\\+");
The problem you got here is that "+" is a meta character in Java, so you need to "scape" it by using "\\+".
As others have mentioned, '+' is special when dealing with regex. In general, if a character is special you can use Pattern.quote() to escape it, as well as "\\+". Documentation on Pattern: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Example:
String string = "Strg-Q";
String[] parts = string.split(Pattern.quote("+"));
String part1 = parts[0]; //Strg
String part2 = parts[1]; //Qenter code here
Remember to import pattern from java.util.regex!
Amit beat me too it. string.split() takes a regular expression as its argument. In regexes + is a special symbol. Escape it with a backslash. Then, since it's a java string, escape the backslash with another backslash, so you get \+. Try testing your regular expressions on an online regex tester.
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]
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.
In my project I used the code to split string like "004*034556" , code is like below :
String string = "004*034556";
String[] parts = string.split("*");
but it got some error and force closed !!
finally I found that if use "#" or another things its gonna work .
String string = "004#034556";
String[] parts = string.split("#");
how can I explain this ?!
Your forgetting something very trivial.
String string = "004*034556";
String[] parts = string.split("\\*");
I recommend you check out Escape Characters.
Use Pattern.quote to treat the * like the String * and not the Regex * (that have a special meaning):
String[] parts = string.split(Pattern.quote("*"));
See String#split:
public String[] split(String regex)
↑
Refer JavaDoc
String[] split(String regex)
Splits this string around matches of the given regular expression.
And the symbol "*" has a different meaning when we talk about Regex in Java
Thus you would have to use an escape character
String[] parts = string.split("\\*");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to split a String by space
I need help while parsing a text file.
The text file contains data like
This is different type of file.
Can not split it using ' '(white space)
My problem is spaces between words are not similar. Sometimes there is single space and sometimes multiple spaces are given.
I need to split the string in such a way that I will get only words, not spaces.
str.split("\\s+") would work. The + at the end of the regular-expression, would treat multiple spaces the same as a single space. It returns an array of strings (String[]) without any " " results.
You can use Quantifiers to specify the number of spaces you want to split on: -
`+` - Represents 1 or more
`*` - Represents 0 or more
`?` - Represents 0 or 1
`{n,m}` - Represents n to m
So, \\s+ will split your string on one or more spaces
String[] words = yourString.split("\\s+");
Also, if you want to specify some specific numbers you can give your range between {}:
yourString.split("\\s{3,6}"); // Split String on 3 to 6 spaces
Use a regular expression.
String[] words = str.split("\\s+");
you can use regex pattern
public static void main(String[] args)
{
String s="This is different type of file.";
String s1[]=s.split("[ ]+");
for(int i=0;i<s1.length;i++)
{
System.out.println(s1[i]);
}
}
output
This
is
different
type
of
file.
you can use
replaceAll(String regex, String replacement) method of String class to replace the multiple spaces with space and then you can use split method.
String spliter="\\s+";
String[] temp;
temp=mystring.split(spliter);
I am giving you another method to tockenize your string if you dont want to use the split method.Here is the method
public static void main(String args[]) throws Exception
{
String str="This is different type of file.Can not split it using ' '(white space)";
StringTokenizer st = new StringTokenizer(str, " ");
while(st.hasMoreElements())
System.out.println(st.nextToken());
}
}