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

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

Related

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

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("^", "\"");

Replace Last Occurrence of a character in a string Using Java [duplicate]

This question already has answers here:
Replace the last part of a string
(11 answers)
Closed 5 years ago.
I am having a string like this
String s1 = "2999.1049.00_GRB.1";
String s2 = "my File.txt.txt";
I want to replace the last ".1" with "_1" and ".txt" with "_txt"
The result of the String should be
s1 = "2999.1049.00_GRB_1" and s2 = "my File.txt_txt"
How can I do this. I am aware of replacing the first occurrence of the string. but don't know how to replace the last occurrence of a string.
Simply use .replace with lastIndexOf method of string
System.out.println(s.replace(s.substring(s.lastIndexOf(".1"), s.length()), "_1"));
You can use regex :
s = s.replaceAll("(.*)\\.(\\d+)$","$1_$2");
// (everything)point(digits) -> (everything)underscore(digits)
It will capture all element before the . in a group (group1), the digit(s) after in another group (group2), and replace by : group1_group2
the first group can be whatever you want
the second group is just digits, even more than 1
Regex demo

Using regex to trim a specific word in String [duplicate]

This question already has answers here:
Delete everything after part of a string
(10 answers)
Closed 6 years ago.
I am not an expert of regex. Suppose I have this string:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
If I want to remove ,normal and replace it by dot so the string becomes like this:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00."
How can I do that in regex?
Thank you very much.
You can use a regular expression like ,\\w+\\.$ which matches any String ending in , a word and then a . and String.replaceAll(String, String) like
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
.replaceAll(",\\w+\\.$", "\\.");
System.out.println(str);
Output is (as requested)
0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00.

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

Categories