This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The split() method in Java does not work on a dot (.)
I'm new to java. I want to split a String from "." (dot) and get those names one by one. But this program gives error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0"
please help me
String input1 = "van.bus.car";
System.out.println(input.split(".")[0]+"");
System.out.println(input.split(".")[1]+"");
System.out.println(input.split(".")[2]+"");
In regex, Dot(.) is a special meta-character which matches everything.
Since String.split works on Regex, so you need to escape it with backslash if you want to match a dot.
System.out.println(input.split("\\.")[0]+"");
To learn more about Regex, refer to following sites: -
http://docs.oracle.com/javase/tutorial/essential/regex/
http://www.vogella.com/articles/JavaRegularExpressions/article.html
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
The argument to split is a regex, and so the full stop/dot/. has a special meaning: match any character. To use it literally in your split, you'll need to escape it:
String[] splits = input1.split("\\.");
That should give you an array of length 3 for your input string.
For more about regex and which characters are special, see the docs for Pattern.
Related
This question already has answers here:
Why does this Java regex cause "illegal escape character" errors?
(7 answers)
Closed 3 years ago.
I want to split a string in java with white spaces. I know that the below line of code does it.
String s[] = str.split("\\\s+");
Here split function takes the regex by which the string must be split. So when I want to split string str through one or more spaces, I should pass \s+ as regex, then why is \\\s+ used?
This will do the split
String s[] = n.split("\\s+");
You don't need a third slash'\' - you get Compile Error.
And first '\' is for escaping the second '\'. Used as an escape character for '\s'.
Like Ismail said, you don't need the third backslash.
In your regex you want to use \s so in Java you also need to escape your backslashes for your tags.
Solution:
Why does this Java regex cause "illegal escape character" errors?
This question already has answers here:
Groovy/Java split string on parentheses "("
(5 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 3 years ago.
I am trying to split a java string with the character "(".
For example :
split("wer(sde")= "wer"+"sde".
But it give exception. Is there a way to split this string using split() function without changing the character "(" to some other character.
String[] cp=cmd.split("{");
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
The thing is, split() receives as parameter a regular expression. Both {} and () are meta-characters and have a special meaning in a regex, so you need to escape them like this:
String[] cp = cmd.split("\\(|\\)");
The method split of String accept a String, that parameter is a regex :
public String[] split(String regex)
Splits this string around matches of the given regular expression.
Since ( is a reserved character in regex, you need to escape it \(.
But in Java, you need to escape twice \\(, once for the String and the second for the regex
This gives :
s.split("\\(");
Parentheses mean something in RegEx, they're used to group characters together. As such, if you intend to reference the literal character, '(' you must escape it within the RegEx:
String[] cp = cmd.split("\\(");
Note the use of two backslashes. This is because the JVM will also interpret a backslash as a metacharacter for escape purposes, so you must escape the backslash itself with another backslash in order for it to make it into the RegEx.
This question already has answers here:
How do I use a delimiter with Scanner.useDelimiter in Java?
(3 answers)
Closed 5 years ago.
I want the scanner to ignore three things: empty spaces, "/" and "!".
What is the correct argument to use in the useDelimiter method?
useDelimiter takes a regex argument docs:
pattern - A string specifying a delimiting pattern
So just make sure the string is in regex form.
Whitespace in regex is \s, escape that to become \\s. / is still / and ! is still !. You then use | to act as an "or" operator to say "either one of these".
Here's how to do it:
scanner.useDelimiter("\\s|/|!");
If you want to say that "consecutive whitespaces slashes and exclamation marks also count as delimiter", then you can add a quantifier + to the whole thing:
scanner.useDelimiter("(\\s|/|!)+");
Scanner's delimiter is just a pattern, so you could use the following:
sc.useDelimiter("[\\s/!]*");
This question already has answers here:
String.split returning null when using a dot
(4 answers)
Closed 9 years ago.
I have to take an input file, and append a number at the end to its name to use as output file. To achieve this, I use the following code:
String delimiter = ".";
String[] splitInput = inputLocation.split(delimiter);
String outputLocation = splitInput[0];
and I get the following exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
I added the following statement to check the length of the splitInput array, and I get 0 as output.
System.out.println(splitInput.length);
Later, I used ".x" as delimiter (my file being .xls). I can use ".x" and achieve my purpose but I'm curious why won't "." work?
The split function uses regular expressions, you have to escape your "." with a "\"
When using regular expressions a "." means any character. Try this
String delimiter = "\\.x";
It should also be mentioned that \ in java is also a special character used to create other special characters. Therefore you have to escape your \ with another \ hence the "\\.x"
Theres some great documentation in the Java docs about all the special characters and what they do:
Java 8 Docs
Java 7 Docs
Java 6 Docs
The . has a special meaning: Any character (may or may not match line terminators). You can escape it prepending \ or use:
[.]x
e.g.:
String delimiter = "[.]x";
See more in http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
String.split() expects a regex as input. In Java regexes, . character is a special character. Thus, your split statement is not working the way you expected. You should escape your "." as \\..
. is considered as any character in regex. Please use escape character \ (which also needs to be escaped as \\), if you want to override the special meaning of it.
This question already has answers here:
Removing a substring between two characters (java)
(3 answers)
Closed 9 years ago.
I want to remove a string that is between two characters and also the characters itself , lets say for example:
i want to replace all the occurrence of the string between "#?" and ";" and remove it with the characters.
From this
"this #?anystring; is #?anystring2jk; test"
To This
"this is test"
how could i do it in java ?
#computerish your answer executes with errors in Java. The modified version works.
myString.replaceAll("#\\?.*?;", "");
The reason being the ? should be escaped by 2 backslashes else the JVM compiler throws a runtime error illegal escape character. You escape ? characters using the backslash .However, the backslash character() is itself a special character, so you need to escape it as well with another backslash.
Use regex:
myString.replaceAll("#\?.*?;", "");
string.replaceAll(start+".*"+end, "")
is the easy starting point. You might have to deal with greediness of the regex operators, however.