How to Replace dot (.) in a string in Java
You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
Replacing all dots in a string with backslashes in Java
Java Strings are made of characters. To allow java programmers to enter strings as 'constants' and part of the Java code, the language allows you to type them in as characters surrounded by '"' quotes.....
String str = "this is a string";
Some characters are hard to type in to the program, like a newline or tab character. Java introduces an escape mechanism to allow the programmer to enter these characters in to a String. The escape mechanism is the '\' backslash.
String str = "this contains a tab\t and newline\n";
The problem is that now there is no easy way to enter a backslash, so to enter the backslash has to escape itself:
String str = "this contains a backslash \\"
The next problem is that Regular Expressions are complicated things, and they also use the backslash \
as an escape character.
Now in, for example, perl, the regular expression \.
would match the exact character '.' because in regular expressions the '.' is special, and needs to be escaped with a '\'. To capture that sequence \.
in a Java program (as a string constant in the program) we will need to escape the '\' as \\
and our Java equivalent regular expression is \\.
. Now, in perl, again, the regular expression to match the actual backslash character is \\
. Similarly, we need to escape both of these in Java in the actual code, and it is \\\\
.
So, the significance here is that the file-separator character in windows is the backslash \
. This single character is stored in the field File.separator. If we want to type the same character in from a Java program, we would have to escape it as \\
, but the '\' is already stored in the field, so we do not need to re-escape it for the Java program, but we DO need to escape it for the regular expression....
There are two ways to escape it for the regular expression. You can elect to add a backslash before it with:
"\\" + File.separator
But this is a bad way to do it because it will not work on Unix (where the separator does not need to be escaped. It is even worse to do what you have done which is to double the file separator:
File.separator+File.separator
The right way to do it is to correctly escape the replacement side of the regular expression with Matcher.quoteReplacement(...)
System.out.println(Test.class.getName().replaceAll("\\.",
Matcher.quoteReplacement(File.separator)) + ".class ")
Remove all the dots but not \in numbers - Java
replaceAll()
with the right regex can do it for you.
This uses a negative look-ahead and look-behind to look for a '.' not in the middle of a decimal number.
rM.replaceAll("(?<![\\d])\\.(?![\\d]+)", "")
replace last dot in a string with a dollar sign
For this requirement, I would use a non-regex solution that can be easier to understand as well as more efficient.
StringBuilder invokedMethSb = new StringBuilder(invokedMeth);
invokedMethSb.setCharAt(invokedMethSb.lastIndexOf("."), '$');
invokedMeth = invokedMethSb.toString();
/*de.java_chess.javaChess.game.GameImpl$GameStatus*/
StringBuilder has some good utils for these operations, such as setCharAt
.
As a personal opinion, I prefer the following one:
char[] invokedArray = invokedMeth.toCharArray();
invokedArray[invokedMeth.lastIndexOf(".")]='$';
invokedMeth = new String(invokedArray);
/*de.java_chess.javaChess.game.GameImpl$GameStatus*/
Regex solution:
You can use the Positive Lookahead, (?=([^.]*)$)
where ([^.]*)
matches any number of non-dot (i.e. [^.]
) character and $
asserts position at the end of a line. You can check regex101.com for more explanation.
public class Main {
public static void main(String[] args) {
String str = "de.java_chess.javaChess.game.GameImpl.GameStatus";
str = str.replaceAll("\\.(?=([^.]*)$)", "\\$");
System.out.println(str);
}
}
Output:
de.java_chess.javaChess.game.GameImpl$GameStatus
Replace from characters with dots
public static String ellipsize(String input, int maxLength) {
if (input == null || input.length() <= maxLength) {
return input;
}
return input.substring(0, maxLength-3) + "...";
}
This method will give string out put with max length maxLength
. replacing all char after MaxLength-3
with ...
eg.
maxLength=10
abc --> abc
1234567890 --> 1234567890
12345678901 --> 1234567...
Java - Regex - Replace multiple dots and commas with just one dot
If you want to replace all .
or ,
by .
:
test.replaceAll("[.,]+", ".");
in Java how can I replace space and dot to only dot?
replaceAll
uses regex for the first arguement. If you want something really simple (aka for the example given, assuming your actual problem isn't a lot more complex) you can just use replace
.
text = text.replace(" .", ".");
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(java.lang.CharSequence, java.lang.CharSequence)
Related Topics
How to Center Crop a Background Image of Linear Layout
Ora-00942 Sqlexception With Hibernate (Unable to Find a Table)
Connect to Remote MySQL Database Through Ssh Using Java
Subscript and Superscript a String in Android
Java Cut String After 2 Decimal Digits
Java.Sql.Sqlexception: Field Doesn't Have a Default Value
How to Count Method Calls by Instance
How to Subtract Number of Days from Current Date in Hql Query
Simplifying the If-Else Condition Using Java Functional Style Programming
How to Find Max Date in List<Object>
Selenium Chromedriver Opens a Blank Page Instead of Url on Linux
Keytool Error Bash: Keytool: Command Not Found
Object Cannot Be Converted to Integer Error
Scheduling a Job With Spring Programmatically (With Fixedrate Set Dynamically)