Split by "?/" does not work - java

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("\\?\\/");

Related

Java String Split on any character (including regex special characters)

I'm sure I'm just overlooking something here...
Is there a simple way to split a String on an explicit character without applying RegEx rules?
For instance, I receive a string with a dynamic delimiter, I know the 5th character defines the delimiter.
String s = "This,is,a,sample";
For this, it's simple to do
String delimiter = String.valueOf(s.charAt(4));
String[] result = s.split(delimiter);
However, when I have a delimiter that's a special RegEx character, this doesn't work:
String s = "This*is*a*sample";
So... is there a way to split the string on an explicit character without trying to apply extra RegEx rules? I feel like I must be missing something pretty simple.
split uses a regular expression as its argument. * is a meta-character used to match zero of more characters in regular expressions, You could use Pattern#quote to avoid interpreting the character
String[] result = s.split(Pattern.quote(delimiter));
You need not to worry about the character type If you use Pattern
Pattern regex = Pattern.compile(s.charAt(4));
Matcher matcher = regex.matcher(yourString);
if (matcher.find()){
//do something
}
You can run Pattern.quote on the delimiter before feeding it in. This will create a string literal and escape any regex specific chars:
delimiter = Pattern.quote(delimiter);
StringUtils.split(s, delimiter);
That will treat the delimiter as just a character, not use it like a regex.
StringUtils is a part of the ApacheCommons library, which is tons of useful methods. It is worth taking a look, could save you some time in the future.
Simply put your delimiter between []
String delimiter = "["+s.charAt(4)+"]";
String[] result = s.split(delimiter);
Since [ ] is the regex matches any characters between [ ]. You can also specify a list of delimiters like [*,.+-]

how to ignore newlines for split function

I am splitting the string using ^ char. The String which I am reading, is coming from some external source. This string contains some \n characters.
The string may look like:
Hi hello^There\nhow are\nyou doing^9987678867abc^popup
when I am splitting like below, why the array length is coming as 2 instead of 4:
String[] st = msg[0].split("^");
st.length //giving "2" instead of "4"
It look like, split is ignoring after \n.
How can I fix it without replacing \n to some other character.
the string parameter for split is interpreted as regular expression.
So you have to escape the char and use:
st.split("\\^")
see this answer for more details
Escape the ^ character. Use msg[0].split("\\^") instead.
String.split considers its argument as regular expression. And as ^ has a special meaning when it comes to regular expressions, you need to escape it to use its literal representation.
If you want to split by ^ only, then
String[] st = msg[0].split("\\^");
If I read your question correctly, you want to split by ^ and \n characters, so this would suffice.
String[] st = msg[0].split("[\\^\\\\n]");
This considers that \n literally exists as 2 characters in a string.
"^" it's know as regular expression by the JDK.
To avoid this confusion you need to modify the code as below
old code = msg[0].split("^")
new code = msg[0].split("\\^")

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.

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("\\.|\\?");

How do I replace all "[", "]" and double quotes in Java

I'm am having difficulty using the replaceAll method to replace square brackets and double quotes. Any ideas?
Edit:
So far I've tried:
replace("\[", "some_thing") // returns illegal escape character
replace("[[", "some_thing") // returns Unclosed character class
replace("^[", "some_thing") // returns Unclosed character class
Don't use replaceAll, use replace. The former uses regular expressions, and [] are special characters within a regex.
String replaced = input.replace("]", ""); //etc
The double quote is special in Java so you need to escape it with a single backslash ("\"").
If you want to use a regex you need to escape those characters and put them in a character class. A character class is surrounded by [] and escaping a character is done by preceding it with a backslash \. However, because a backslash is also special in Java, it also needs to be escaped, and so to give the regex engine a backslash you have to use two backslashes (\\[).
In the end it should look like this (if you were to use regex):
String replaced = input.replaceAll("[\\[\\]\"]", "");
The replaceAll method is operating against Regular Expressions. You're probably just wanting to use the "replace" method, which despite its name, does replace all occurrences.
Looking at your edit, you probably want:
someString
.replace("[", "replacement")
.replace("]", "replacement")
.replace("\"", "replacement")
or, use an appropriate regular expression, the approach I'd actually recommend if you're willing to learn regular expressions (see Mark Peter's answer for a working example).
replaceAll() takes a regex so you have to escape special characters. If you don't want all the fancy regex, use replace().
String s = "[h\"i]";
System.out.println( s.replace("[","").replace("]","").replace("\"","") );
With double quotes, you have to escape them like so: "\""
In java:
String resultString = subjectString.replaceAll("[\\[\\]\"]", "");
this will replace []" with nothing.
Alternatively, if you wished to replace ", [ and ] with different characters (instead of replacing all with empty String) you could use the replaceEachRepeatedly() method in the StringUtils class from Commons Lang.
For example,
String input = "abc\"de[fg]hi\"";
String replaced = StringUtils.replaceEachRepeatedly(input,
new String[]{"[","]","\""},
new String[]{"<open_bracket>","<close_bracket>","<double_quote>"});
System.out.println(replaced);
Prints the following:
abc<double_quote>de<open_bracket>fg<close_bracket>hi<double_quote>

Categories

Resources