Replace letter after specified character (including specified character) (all instances) java String - java

I want to replace every "§" and the character after it with nothing.
Example:
"§a§b Text"
Goes to:
" Text"
§a turns to nothing because there is the character §. Same with §b.
How would I go about doing this?
I know about String.replaceAll(), but since the second character can be anything, I'm not sure how I would get rid of it.

Use a dot for the "any character" part:
str = str.replaceAll("§.", "");

Related

How to check and replace a sequence of characters in a String?

Here what the program is expectiong as the output:
if originalString = "CATCATICATAMCATCATGREATCATCAT";
Output should be "I AM GREAT".
The code must find the sequence of characters (CAT in this case), and remove them. Plus, the resulting String must have spaces in between words.
String origString = remixString.replace("CAT", "");
I figured out I have to use String.replace, But what could be the logic for finding out if its not cat and producing the resulting string with spaces in between the words.
First off, you probably want to use the replaceAll method instead, to make sure you replace all occurrences of "CAT" within the String. Then, you want to introduce spaces, so instead of an empty String, replace "CAT" with " " (space).
As pointed out by the comment below, there might be multiple spaces between words - so we use a regular expression to replace multiple instances of "CAT" with a single space. The '+' symbol means "one or more",.
Finally, trim the String to get rid of leading and trailing white space.
remixString.replaceAll("(CAT)+", " ").trim()
You can use replaceAll which accepts a regular expression:
String remixString = "CATCATICATAMCATCATGREATCATCAT";
String origString = remixString.replaceAll("(CAT)+", " ").trim();
Note: the naming of replace and replaceAll is very confusing. They both replace all instances of the matching string; the difference is that replace takes a literal text as an argument, while replaceAll takes a regular expression.
Maybe this will help
String result = remixString.replaceAll("(CAT){1,}", " ");

Need to remove special characters from strings written in a file in java

I have a text file which contains data. Some special char comes in the file. I need to remove all "special" characters, ie:
],à,>,¤,`,ƒ,Š,¥,Œ,^,>¤,°,ã,Ãé,–«»°,NÂ,N,º,?¿Ññ,ß,ä,º,ô5,ª,é ,ª,§,Á
These need to be replaced with a space chat, not removed.
I have one constraint that I have to store output in a String, because I need to pass that string further in TIBCO. I have written the following code but it is removing everything. As I need to have + and - symbol in file.
str = str.replaceAll("[^\\w\\s]*", "");
Any help appreciated.
Firstly, if you need to replace with whitespace and not with blank, why are you replacing with blank?
You could just use a white list of all chars you want to keep by adding plus and minus signs to the character class:
.replaceAll("[^\\w\\s.,+-]", " ")
I also added the dot and comma, since you probably want these too.
But it looks like a blanket character would be better, since all chars you don't want are above 127:
.replaceAll("[\u0080-\uffff]", " ")
You can add other chars you don't want to this character class as you need.
Note: In both cases, I removed the quantifier *, because you want a 1-for-1 replacement. If you use * the regex will match between every character, and match a sequence of unwanted chars, which will mess up your file.

Remove unwanted characters from string by regex in Java

I have a string here:
javax.swing.JLabel[,380,30,150x25,alignmentX=0.0,alignmentY=0.0]: Hello
I want to remove everything before the ":", including the ":" itself. This would leave only "Hello". I read about regex, but no combination I tried worked. Can someone tell me how to do it. Thanks in advance!
You need to use replaceAll method or replaceFirst.
string.replaceFirst(".*:\\s*", "");
or
string.replaceAll(".*:\\s*", "");
This would give you only Hello. If you remove \\s* pattern,then it would give you <space>Hello string.
.* Matches any character zero or more times, greedily.
: Upto the colon.
\\s* Matches zero or more space characters.
You could also just split the string by : and take the second string. Like this
String sample = "javax.swing.JLabel[,380,30,150x25,alignmentX=0.0,alignmentY=0.0]: Hello";
System.out.println(sample.split(":", -1)[1]);
This will output
<space>Hello
If you want to get rid of that leading space just trim it off like
System.out.println(sample.split(":", -1)[1].trim());

Regular Expression: Replace except from specific characters and whitespace

I am coding in Java and I have a string where I want to keep letters, digits, ":", "-" and whitespaces and remove everything else. So, I have used this piece of code:
str=str.replaceAll("[^\\dA-Za-z#:-\\s*]", "");
It doesn't work.
It does work fine until
str=str.replaceAll("[^\\dA-Za-z#:-]", "");
where everything else, except from letters, digits and the characters ":" and "-" is removed
But when I am trying to add the condition for whitespace characters I am facing problems.
I would appreciate your help.
Thank you in advance.
- when used within character class depicts range..
In your case you were actually trying to match characters from range : to \s which is an invalid range..
Move - to the start
[^-\\dA-Za-z#:\\s]
or end
[^\\dA-Za-z#:\\s-]
The dash must be the first or last character in a character class, or it will be interpreted as a range indicator (as in [A-Z]); in your case [:-\\s] is a meaningless range. Use
str = str.replaceAll("[^\\dA-Za-z#:\\s-]+", "");
(or did you want to keep asterisks in your text, too)?

Regular expressions: all words after my current one are gone

I need to remove all strings from my text file, such as:
flickr:user=32jdisffs
flickr:user=acssd
flickr:user=asddsa89
I'm currently using fields[i] = fields[i].replaceAll(" , flickr:user=.*", "");
however the issue with this is approach is that any word after flickr:user= is removed from the content, even after the space.
thanks
You probably need
replaceAll("flickr:user=[0-9A-Za-z]+", "");
flickr:user=\w+ should do it:
String noFlickerIdsHere = stringWithIds.replaceAll("flickr:user=\\w+", "");
Reference:
\w = A word character: [a-zA-Z_0-9]
Going by the question as stated, chances are that you want:
fields[i] = fields[i].replaceAll(" , flickr:user=[^ ]* ", ""); // or " "
This will match the string, including the value of user up to but not including the first space, followed by a space, and replace it either by a blank string, or a single space. However this will (barring the comma) net you an empty result with the input you showed. Is that really what you want?
I'm also not sure where the " , " at the beginning fits into the example you showed.
The reason for your difficulties is that an unbounded .* will match everything from that point up until the end of the input (even if that amounts to nothing; that's what the * is for). For a line-based regular expression parser, that's to the end of the line.

Categories

Resources