How to remove dots from a string in Java? [duplicate] - java

This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 3 years ago.
In my program I want to remove all the dots(.) I've tried to do this but it's not working.
public class MyClass {
public static void main(String args[]) {
String str = " .Hello.World..Awesome!. ";
System.out.println(str.replaceAll(".",""));
}
}

Replace all takes a regex as the first argument and you need to escape "." like
"\\."
str.replaceAll("\\.","")

This is not working because . is a special regex character. You must escape this using a backslash. The dot is used as a capture all in regex.
System.out.println(str.replaceAll("\\.",""));
You can read up on all of the special characters in regex here - https://www.regular-expressions.info/characters.html

Related

Regex for split method in String class which split the text by the condition [duplicate]

This question already has answers here:
Regex to match only commas not in parentheses?
(3 answers)
Closed 3 years ago.
I've the string looking like this:
word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?|word1-word2-\d{1,2}\w?-\d{1,2}\w?|word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w
I'd like to split this string by '|' everywhere where it doesn't precedes by '('. So result should be:
["word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w"]
I've trying to use negative lookahead \((?!\|) which split the text to on '('.
UPDATE
So I want to achieve not splitting the "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w" on '|' where that character is precedes by '('.
Could someone please help me with this?
Code:
public static void main(String[] args) {
String str = "word1-word2-word3-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-word3-(\\d{1,2}\\w?\\d{1,2}|\\d{1,2}\\w?)-\\d{1,2}\\w";
String[] arrOfStr = str.split("\\|");
for (String a : arrOfStr)
System.out.println(a);
}

Java replaceAll with parenthesis [duplicate]

This question already has an answer here:
using parenthesis replaceAll method Java
(1 answer)
Closed 7 years ago.
In the code below I'm trying to replace in text occurrences of fromString withtoString, but no replacement takes place. How to set the parenthesis in the regex to make this work?
public static void main(String[] args) {
String fromString = "aaa(bbb)";
String toString = "X";
String text = "aaa(bbb) aaa(bbb)";
String resultString = text.replaceAll(fromString, toString);
System.out.println(resultString);
}
replaceAll uses regex as its first argument. Parenthesis () are used for capturing groups so need to be escaped
String fromString = "aaa\\(bbb\\)";
Since you can't modify the input String you can use Pattern.quote
String resultString = text.replaceAll(Pattern.quote(fromString), toString);
or simply String.replace could be used which doesnt use regex arguments
String resultString = text.replace(fromString, toString);

Java replaceAll("\\s+") vs replaceAll("\\\\s+") [duplicate]

This question already has an answer here:
regex: How to escape backslashes and special characters?
(1 answer)
Closed 8 years ago.
What's the difference between replaceAll("\\s+") and replaceAll("\\\\s+")? Usually I use \\s+ but sometimes I see \\\\s+.
\\s+ --> replaces 1 or more spaces.
\\\\s+ --> replaces the literal \ followed by s one or more times.
Code:
public static void main(String[] args) {
String s = "\\sbas def";
System.out.println(s);
System.out.println(s.replaceAll("\\s+", ""));
System.out.println(s.replaceAll("\\\\s+", ""));
}
O/P :
\sbas def
\sbasdef
bas def

why is my string.length 0 if I split this string? [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 8 years ago.
This is probably a very easy question but I'm going to give you the code first.
import java.util.Scanner;
public class help {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Give: ");
String s = sc.next();
String[] parts = s.split(".");
System.out.println(parts.length);
}
}
Even if I give 192.168.1.1.1.1.1 or 1.2.3 or ... the parts.length will always be 0, can somebody please explain to me why and how I can let it be 4 if i enter 1.2.3.4?
You need s.split("\\.") because the argument to split is a regular expression. The . character in a regular expression means "any character", and you need to escape it with the backslash to have it mean "dot".
Because "." is a special character, meaning "any character".
You need to escape it to be able to use it as the character ".":
String[] parts = s.split("\\.");

empty array, split method [duplicate]

This question already has answers here:
The split() method in Java does not work on a dot (.) [duplicate]
(7 answers)
Closed 9 years ago.
My problem is that the array ms[ ] doesn't get values when I do split( );
Why is this happening ?
public class Test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss."); //change format
String msgTime = ft.format(date);
System.out.println(msgTime);
String ms[] = msgTime.split(".");
System.out.println(ms.length);
}
}
The problem is split() function takes regular expression as argument, not a simple string. And "." regular expression means "any symbol".
So you need just escape it.
String ms[] = msgTime.split("\\.");
I'm guessing you meant to do
String ms[] = msgTime.split("\\.");
String.split() takes a regular expression so you should escape any special characters, such as ..

Categories

Resources