Getting " when trying to replace a character in string [duplicate] - java

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
How do you match a caret (^) symbol in regex?
(2 answers)
Carets in Regular Expressions
(2 answers)
Closed 4 years ago.
I want to replace " from a string with ^.
String str = "hello \"there";
System.out.println(str);
String str1 = str.replaceAll("\"", "^");
System.out.println(str1);
String str2= str1.replaceAll("^", "\"");
System.out.println(str2);
and the output is :
hello "there
hello ^there
"hello ^there
why I am getting extra " in start of string and ^ in between string
I am expecting:
hello "there

the replaceAll() method consume a regex for the 1st argument.
the ^ in String str2= str1.replaceAll("^", "\""); will match the starting position within the string.
So if you want the ^ char, write \^
Hope this code can help:
String str2= str1.replaceAll("\\^", "\"");

Try using replace which doesnt use regex
String str2 = str1.replace("^", "\"");

^ means start of a line in regex, you can add two \ before it:
String str2= str1.replaceAll("\\^", "\"");
The first is used to escape for compiling, the second is used to escape for regex.

Since String::replaceAll consumes regular expression you need to convert your search and replacement strings into regular expressions first:
str.replaceAll(Pattern.quote("\""), Matcher.quoteReplacement("^"));

Why do you want to use replaceAll. Is there any specific reason ?
If you can use replace function then try below
String str2= str1.replace("^", "\"");

Related

Remove all occurrences of char "." from string in Java [duplicate]

This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 4 years ago.
I would like to remove the period/decimal character from a String using Java.
String originalString = "1.2345";
originalString = originalString.replaceAll(".", "");
Printing originalString returns empty.
How can I remove . from originalString?
The first argument of replaceAll is a regex pattern. Since . means "any character", all the characters are removed. In order to refer to the actual . character, you need to escape it:
originalString = originalString.replaceAll("\\.", "");
String originalString = "1.2345";
originalString = originalString.replaceAll("\\.", "");

String.split() not working with "[]" [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 have a IPv6 string
String str = "demo1 26:11:d0a2:f020:0:0:0:a3:2123 demo2";
String searchString = "26:11:d0a2:f020:0:0:0:a3:2123";
When i use str.split(searchString) code returns
["demo1 ", " demo2"]
Which is fine but when i use:
String str = "demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2";
String searchString = "[26:11:d0a2:f020:0:0:0:a3]:2123";
and do str.split(searchString) it reutrns
[demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2]
Which is wrong i guess , can some one tell why I am getting this sort of output?
Since split function takes a regex as parameter, you need to escape those brackets otherwise this [26:11:d0a2:f020:0:0:0:a3] would match a single character only.
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
str.split(searchString);
It is happening because split(String str) take regex pattern string as argument. And that string will be used as regex pattern to match all the delimiter with this pattern.
In your regex pattern you are providing character sets in [].
To make it work your way you will have to use this regex pattern string :
\[26:11:d0a2:f020:0:0:0:a3\]:2123
i.e. in java :
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
I hope you are familiar with the string regexs. In java, the regex [abc] means match with a OR b OR c I encourage you to escape your square brackets try:
String str = "demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2";
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
You have to use an escape sequence for some special characters. Use \\[ ... \\] in the searchString variable.

JAVA string split with regex ("||") cannot be used? [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a string like |serialNo|checkDelta?|checkFuture?|checkThis?|.
Now I am using the following code to split the string.
String[] splitString = str.split("|");
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo, checkDelta?, checkFuture?, checkthis?.
How to get these? Am I missing something?
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
You are using a special character and will have to escape it: str.split("\\|");
Use StringTokenizer..
String str="|serialNo|checkDelta?|checkFuture?|checkThis?|"
StringTokenizer st=new StringTokenizer(str,"|",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
s1=serialNo
s2=checkDelta?
s3=checkFuture?
s4=checkThis?
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

Using split() with "|" character [duplicate]

This question already has answers here:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Closed 9 years ago.
Just a quick question:
String input = "sam|y|rutgers";
String[] splitInput = input.split("|");
System.out.println(Arrays.toString(splitInput));
Output:
[, s, a, m, |, y, |, r, u, t, g, e, r, s]
I would like to split at the pipe characters to get
[sam,y,rutgers]
Any idea what I'm doing wrong here?
Try with one of these
split("\\|")
split("[|]")
split(Pattern.quote("|"))
split("\\Q|\\E")
split method uses regex as parameter and in regex | means OR so your current expression means empty string or empty String.
If you want to make | simple literal you need to escape it. To do this you can
place \ before it in regex engine which in String will be written as "\\|".
use character class [...] to escape most of regex metacharacters like split("[|]")
or surround your special characters with \\Q and \\E which will make every character (regardless if it is special or not) simple literal. This solution is used in Pattern.quote("regex").
You may try this:
String[] str= input.split("\\|");
"|" is a special character(OR) so you need to escape that using a slash \\. As answered here An unescaped | is parsed as a regex meaning "empty string or empty string,"
\Q & \E are regex quotes.
String[] splitInput = input.split("\\Q|\\E");
You can do it by StringTokenizer
StringTokenizer st2 = new StringTokenizer(input , "|");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
default delimeter is " " space.
StringTokenizer st2 = new StringTokenizer(input );//it will split you String by `" "` space

How to split a string on | (pipe) in Java [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a string like |serialNo|checkDelta?|checkFuture?|checkThis?|.
Now I am using the following code to split the string.
String[] splitString = str.split("|");
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo, checkDelta?, checkFuture?, checkthis?.
How to get these? Am I missing something?
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
You are using a special character and will have to escape it: str.split("\\|");
Use StringTokenizer..
String str="|serialNo|checkDelta?|checkFuture?|checkThis?|"
StringTokenizer st=new StringTokenizer(str,"|",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
s1=serialNo
s2=checkDelta?
s3=checkFuture?
s4=checkThis?
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

Categories