| not recognized in java string.split() method [duplicate] - java

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 8 years ago.
I am having problems with the java string.split method.
I have a string word like so, which equals- freshness|originality. I then split this string like so:
String words[] = word.split("|");
If I then output words[1], like so:
t1.setText(words[1]);
It gives me the value f. I have worked out that this is the f in the word freshness.
How can I split the string properly so that words[1] is actually originality? Thanks for the help!

You should escape it:
String words[] = word.split("\\|");
Check this explanation in similar question here: Why does String.split need pipe delimiter to be escaped?
String object's split() method has a regular expression as a parameter. That means an unescaped | is not interpreted as a character but as OR and means "empty string OR empty string".

You need to escape the pipe because java recognizes it as a Regular Expression OR Operator.
line.split("\\|")
"|" gets is parsed as "empty string or empty string," which isn't what you are trying to find.
For the record
... ? . + ^ : - $ *
are all Regex Operators and need to be escaped.

You need to escape the character. Use "\\|".
More information on regex escaped characters here.

String test ="freshness|originality";
String[] splits = test.split("\\|");
String part1 = splits[0]; // freshness
String part2 = splits[1]; // originality

Related

Splitting a string in which delimiting string ^^^^ has special meaning for regex [duplicate]

This question already has answers here:
Java split on ^ (caret?) not working, is this a special character?
(5 answers)
Closed 3 years ago.
Trying to split a string like this
12^^^^John Doe^^^^+1897987987
delimiter is the ^^^^
split method does not split and instead returns entire line and single length array
String[] line = str.split("^^^^");
id = line[0];
// code below does not work because there is only one element in array
name = line[1];
number = line[2];
^ has a special meaning in regex so I believe this split params have to be passed in a different way to achieve the desired result, please advise.
You're going to need to escape those characters - ^ means "start of line" in a regex.
String[] line = str.split("\\^\\^\\^\\^");
Two \ are needed because otherwise Java tries interpreting \^ as a string escape character, like \n or \t.

How to split a java string with "("? [duplicate]

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.

Java String Split Behavior - Split on Dollar Symbol [duplicate]

This question already has answers here:
Why can't I split a string with the dollar sign?
(6 answers)
Closed 7 years ago.
I am trying the following code (running java version 1.7 in Eclipse Luna IDE on Ubuntu Linux 12.04):
String str = "abc$xyz";
String[] split_ = str.split("$");
System.out.println(split_.length);
I am always getting a split of length 1. If I try to print split_[0], I am always getting the entire string. Can anyone suggest what might be the cause?
This is because split expects a regular expression. Since "$" is the end-of-line marker in a regular expression, this only splits on the end of the String.
You should use
String str = "abc$xyz";
String[] split_ = str.split("\\$");
System.out.println(split_.length);
instead.
This escapes the "$", so that it's treated as a literal character instead (and uses two slashes to escape the backslash as part of the string literal).
The $ character is a metacharacter meaning "end of line", not a literal dollar sign.
Escape the $ character with two backslashes, one to escape the $ in the regular expression, one for a Java escape for a backslash.
String[] split_ = str.split("\\$");
.split() uses regex that is why...
Try this:
String str = "abc$xyz";
String[] split_ = str.split("\\$");
System.out.println(split_.length);

replaceAll Dangling metacharacter [duplicate]

This question already has answers here:
replace String with another in java
(7 answers)
Closed 7 years ago.
I have a string, say 1++++----2 and I want to replace ++++---- with a certain string, say string.
The I use the java function replaceAll, but it keep warning Dangling metacharacter every time I use it:
mystring.replaceAll("++++----", "string");
Escape the +, only the first or all doesn't matter here.
String str = "1+++---2";
str = str.replaceAll("\\+\\+\\+---", "");
System.out.println(str);
Output:
12
Or use replaceAllas it's meant to be used:
str = str.replaceAll("[+-]", "");
replaceAll's first argument takes a regualr expression and + have special meaning in regualr expression. Escape + to make it work properly .
mystring.replaceAll("\\+\\+\\+\\+----", "string");
You can use following regex :
mystring.replaceAll("\\++-+", "string")
Since + is a regex character you need to escape it.so here in "\\++-+" the first part \\+ will match the character + literally and the second + will match 1 or more combination of character + and the rest is -+ which will match 1 or more -.
When you replace a fixed string, you need a replace function:
String mystring = "1++++----2";
System.out.println(mystring.replace("++++----", "string"));
See demo
Otherwise, you need a regex with replaceAll.
System.out.println(mystring.replaceAll("[+]+-+", "string"));
Note that you do not need to escape the + inside a regex character class, which is convenient in Java.

Splitting a Java String return empty array? [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 9 years ago.
I have a String something like this
"myValue"."Folder"."FolderCentury";
I want to split from dot("."). I was trying with the below code:
String a = column.replace("\"", "");
String columnArray[] = a.split(".");
But columnArray is coming empty. What I am doing wrong here?
I will want to add one more thing here someone its possible String array object will contain spitted value like mentioned below only two object rather than three.?
columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
Note that String#split takes a regex.
You need to escape the special char . (That means "any character"):
String columnArray[] = a.split("\\.");
(Escaping a regex is done by \, but in Java, \ is written as \\).
You can also use Pattern#quote:
Returns a literal pattern String for the specified String.
String columnArray[] = a.split(Pattern.quote("."));
By escaping the regex, you tell the compiler to treat the . as the string . and not the special char ..
You must escape the dot.
String columnArray[] = a.split("\\.");
split() accepts an regular expression. So you need to skip '.' to not consider it as a regex meta character.
String[] columnArray = a.split("\\.");
While using special characters need to use the particular escape sequence with it.
'.' is a special character so need to use escape sequence before '.' like:
String columnArray[] = a.split("\\.");
The next code:
String input = "myValue.Folder.FolderCentury";
String regex = "(?!(.+\\.))\\.";
String[] result=input.split(regex);
System.out.println(Arrays.toString(result));
Produces the required output:
[myValue.Folder, FolderCentury]
The regular Expression tweaks a little with negative look-ahead (this (?!) part), so it will only match the last dot on a String with more than one dot.

Categories

Resources