how to trim specific characters off of Strings on Java [duplicate] - java

This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 2 years ago.
I'd like to trim any single trailing . or -. I tried doing this by doing something like "f-o.o.".replaceFirst("^(\\.+)[-|.]$", "$0"). The expected string is f-o.o but I'm getting f-o.o.. Thank you.

Your expression has two mistakes:
you put a slash in front of a dot, making it match a literal dot, not just any character
you put | into a character class, so your expression would remove not just . or - at the end of the string, but also |.
Use "f-o.o.".replaceFirst("[-.]$", "")

Related

Replacing special characters in Java String [duplicate]

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!

how To change my regex to reject underscores [duplicate]

This question already has answers here:
Regular Expressions: How to Express \w Without Underscore
(7 answers)
Closed 2 years ago.
Currently have this regex string in my java code:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It successfully accepts those characters, however it also accepts underscore, how can modify the regex to reject underscore appearing anywhere in the string?
Your regex is:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It is using \w which is equivalent of [a-zA-Z0-9_], hence it allows underscore also.
You can change your character class to this:
^[-#. a-zA-Z0-9\\/]{0,70}$
Note that space, dot, #, / don't need to be escaped inside [...] and - if placed at first or last position doesn't require escaping either.

Correct argument for useDelimiter to ignore empty space as well as two specific signs [duplicate]

This question already has answers here:
How do I use a delimiter with Scanner.useDelimiter in Java?
(3 answers)
Closed 5 years ago.
I want the scanner to ignore three things: empty spaces, "/" and "!".
What is the correct argument to use in the useDelimiter method?
useDelimiter takes a regex argument docs:
pattern - A string specifying a delimiting pattern
So just make sure the string is in regex form.
Whitespace in regex is \s, escape that to become \\s. / is still / and ! is still !. You then use | to act as an "or" operator to say "either one of these".
Here's how to do it:
scanner.useDelimiter("\\s|/|!");
If you want to say that "consecutive whitespaces slashes and exclamation marks also count as delimiter", then you can add a quantifier + to the whole thing:
scanner.useDelimiter("(\\s|/|!)+");
Scanner's delimiter is just a pattern, so you could use the following:
sc.useDelimiter("[\\s/!]*");

Regex to identify more than 1 occurance of underscore consequtively [duplicate]

This question already has answers here:
Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
(32 answers)
Closed 6 years ago.
I have string in which more than one underscore can be present together.
For example : this_is__Dummy_____String
I have to replace this more than one occurance by only one underscore so that target string should look like :
this_is_Dummy_String
Thanks in advance !
You can use String#replaceAll to replace the undescores.
"this_is__Dummy_____String".replaceAll("_{2,}", "_")
The given regex will replace all occurences of "two or more" underscores with a single underscore.

Remove trailing zeros from string java [duplicate]

This question already has answers here:
Regex for removing trailing zeros
(5 answers)
Closed 7 years ago.
Although I have seen a question similar to this one asked quite a few times, I actually mean remove all trailing zeroes.
I would like to convert something like
"1903895810000"
to
"190389581"
I am looking for a String.replace() solution
Simple regexp with replaceAll will do it.
String s = "1903895810000";
System.out.println(s.replaceAll("0+$", ""));
[EDIT]:
s.replace(0, "") will not work here, because it will remove all zeros from the string, so you can't use it. So, here I used replaceAll, that uses regular expressions to match replacement string. This simple regexp 0+$ matches any number of zeros 0+ followed by end-of-string $, so it would be "some zeroes at the end".

Categories

Resources