This question already has answers here:
Trim characters in Java
(14 answers)
Trim leading or trailing characters from a string?
(6 answers)
Closed 5 years ago.
Consider this code:
string xx = "/test/file2/".Trim('/');
Console.WriteLine(xx);
Console.Read();
This code returns test/file2. What is an efficient way to do this in Java?
Here is how to do the same in Java:
String xx = "/test/file2/".replaceAll("(^/*|/*$)", "");
This uses a regex that matches multiple / from the beginning or multiple / at the end and replaces them with void.
Related
This question already has answers here:
Alternation operator inside square brackets does not work
(2 answers)
What is the difference between square brackets and parentheses in a regex?
(3 answers)
Closed 2 years ago.
I want to replace all special characters in the string shown below:
String a="Test’‵"
I want to replace ’ and ‵ with dashes (-). I have tried the following:
a=a.replaceAll("[’|‵]", "-");
This generates the following result:
Test------
instead of
Test--
How can I achieve the desired result?
Don't use square brackets, as it represents a set of single characters to match (a character class).
a=a.replaceAll("’|‵", "-");
Demo!
This question already has answers here:
Replacing last character in a String with java
(13 answers)
Closed 3 years ago.
I got a string (URL) which ends with "hl=en", "hl=ru", "hl=it" etc with other languages. I want to replace (always) last 5 symbols to "hl=en". What kind of regex should I use?
Well if you really want to use Regex you could use:
str.replaceAll(".{5}$", "hl=en");
Will get 5 characters followed by a 'end of line/string' character and replace it with the desired string
This question already has answers here:
Why is String.matches returning false in Java?
(3 answers)
Closed 5 years ago.
Pattern.matches("[A(BC)]", "BC") why this returns false?
Because the pattern expects to see a single character from the class A(BC), and matches matches the entire input against the regex (doesn't look for partial matches). Since the input is two characters, it isn't a match.
This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 6 years ago.
I try to split "." but it can not work, I get strings.length equals 0 .What's wrong with it?
String string = "11.12.1";
String[] strings = string.split(".");
As string split takes an regex as argument, . is a wildcard for any character. Just escape it using a backslash (which you have to also escape for java with another one). Additionally, as Youcef Laidani pointed out, you have to call split on the string you just created, not something else:
string.split("\\.");
This question already has answers here:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Split a String on | (pipe) in Java [duplicate]
(3 answers)
Closed 8 years ago.
I'm trying to split the following: 116,24|426,776 into two strings upon the | symbol.
My code:
String[] splitString = "116,24|462,776".split("|");
System.out.println(splitString[1]);
However, this code simply returns: 1
Any clues?
You have to use escape character.
Try this String[] splitString = "116,24|462,776".split("\\|");
Here \\ is the escape character.
Please refer http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html