How can I split this? - java

I want to split function in java.But it s not working good.
String mystring = "ersin#$gulbahar#$ev";
String [] scripts= mystring.split("#$"); //it does not split.
how can i fix this?

String mystring = "ersin#$gulbahar#$ev";
String[] scripts = mystring.split("#\\$");
System.out.println(Arrays.toString(scripts));
OUTPUT:
[ersin, gulbahar, ev]

try this:
mystring.split("#\\$")
the split method uses a Regex to split the text, a $ character has other mean in a regex

split takes a regular expression as a parameter, and $ is a special character in a regular expression meaning "match the end of the string".
Since you want to match a literal $, not the end of the string, you need to escape it with a backslash: mystring.split("#\\$"); should work.

Escape $ in your expression: split() takes a regular expression as parameter! Common issue...

Related

Printing Strings in Java using replaceAll

I am a beginner in Java. I don't understand how the below code is able to print all the characters in a string:-
System.out.println(yourString.replaceAll(".", "$0\n"));
I have tried reading the documentation on replaceAll and regex, still no clue.
"." is a regular expression which matches any single character. $0 in the replacement string is a placeholder for the full match of the regex. \n is a line break.
Summarized, this snippet replaces each character with itself and adds a line break after the character.
The syntax for replaceAll() method is as follows:
replaceAll(String regex, Stringreplacement) where:
regex : regular expression
replacement : replacement sequence of characters
so when you what to replace a character with \n basically every character will be printed in a different line. For example: yourString = "Hello." =>
output: Hello with every character on a different line
If the String (as you specified) is String yourString = "-"; so the result of System.out.println(yourString.replaceAll(".", "$0\n")); will be "-\n".
Actually, if you need to print all of the String characters why are you using replaceAll? Coz System.out.println(yourString); will do it perfectly.

Java split method is not working properly?

This is my piece of code I want to split the string with $ symbol but the string doesn't getting spitted.
Here is my code:
String str="first$third$nine%seventh";
String s[]=str.split("$");
System.out.println(s[0]);
The output is the whole string:
first$third$nine%seventh
split takes a regular expression as an argument. $ is a magic character in regex.
If you escape it with backslashes, it will be used as a normal character instead of a special regex character.
String s[]=str.split("\\$");
This is a very common thing in string class. Ans this has been already asked and answers are available in stackoverflow.
You should escape the regular expression symbol in split method. There are so many characters like $,?,*,^,+ which should be escaped while using as a parameter in split method.

Split by "?/" does not work

I want to split a string by: "?/". My string is: hello?/hi/hello.
My code is:
String [] list=myString.split("/?/");
My output is: [HELLO,hi,hellow] but I want to see: [hello,hi/hello].
How can I do that?
You need to escape ? otherwise it is interpreted as a meta character.
The simplest pattern to meet your needs is:
String[] list = myString.split("\\?/");
If you're not familiar with regular expressions, you can let Pattern.quote() do the work for you: it accepts a string and escapes any pesky special characters that would otherwise break your literal split expression:
String[] list = myString.split(Pattern.quote("?/"));
Try this
String [] list = myString.split("\\?/");
Your regexp should rather be "\\?/" (? needs to be escaped with a \)
System.out.println(Arrays.toString("hello?/hi/hello".split("\\?/")))
the split mechanism takes regular expressions as input so you need to escape special characters with a double backslash (which will escape to a single backslash within the string)
String [] list = myString.split("\\?\\/");

Java Split strings on "." and "?"

public static void main(String[] args){
String testStr = "Test test. Test 2. Test3?";
String[] newStr = testStr.split(".?");
System.out.print(newStr[0]);
}
I get an Array Index Out of Bounds exception running this, and the length of newStr is 0. I want to break the sentence up into tokens on "." and "?". What error am I making?
String#split takes a regular expression as a parameter to split. . and ? have special meaning in regular expressions.
You can use character class to split on either of them:
testStr.split("[.?]");
Inside a character class you don't need to escape them, as special characters lose meaning inside character class.
If you use it with normal pipe |, you need to escape it: -
testStr.split("\\.|\\?");
UPDATE: - If you want to preserve your delimiters:
testStr.split("(?<=[.?])")
You must escape those characters. Use "\\." and "\\?". This is because String#split receives a regular expression, and both characters are special characters in regex.
You need to escape . and ?. Use :
String[] newStr = testStr.split("\\.|\\?");

Java and string split

split this String using function split. Here is my code:
String data= "data^data";
String[] spli = data.split("^");
When I try to do that in spli contain only one string. It seems like java dont see "^" in splitting. Do anyone know how can I split this string by letter "^"?
EDIT
SOLVED :P
This is because String.split takes a regular expression, not a literal string. You have to escape the ^ as it has a different meaning in regex (anchor at the start of a string). So the split would actually be done before the first character, giving you the complete string back unaltered.
You escape a regular expression metacharacter with \, which has to be \\ in Java strings, so
data.split("\\^")
should work.
You need to escape it because it takes reg-ex
\\^
Special characters like ^ need to be escaped with \
This does not work because .split() expects its argument to be a regex. "^" has a special meaing in regex and so does not work as you expect. To get it to work, you need to escape it. Use \\^.
The reason is that split's parameter is a regular expression, so "^" means the beginning of a line. So you need to escape to ASCII-^: use the parameter "\\^".

Categories

Resources