In a string, how can I replace all "0" with X and all "00" with Y, but if it's more than 2 zeros just leave it as is.
For Example: 00 0 000 0000 0
Example output: Y X 000 0000 X
In a string, how can I replace all "0" with X and all "00" with Y, but if it's more than 2 zeros just leave it as is.
For Example: 00 0 000 0000 0
Example output: Y X 000 0000 X
In Java 14+, you can do it like this:
String input = "00 0 000 0000 0";
String result = Pattern.compile("0+").matcher(input).replaceAll(m ->
switch (m.group().length()) {
case 1 -> "X";
case 2 -> "Y";
default -> m.group();
});
System.out.println(result); // prints: Y X 000 0000 X
You could solve this with regular expressions
String listOfNumbers = "00 0 000 0000 0";
listOfNumbers = listOfNumbers.replaceAll("(^| )00( |$)", " Y ");
listOfNumbers = listOfNumbers.replaceAll("(^| )0( |$)", " X ");
listOfNumbers = listOfNumbers.trim();
System.out.println(listOfNumbers);
This code prints out Y X 000 0000 X
How it works: First the code checks to replace all occurrences of 00, next it will take the intermediate result and wil check for 0
(^| ): We either expect the start of the String, or we expect a blank space character00: We expect 2 occurrences of the 0 character( |$): Here we expect a blank space, or the end of the Stringtrim() with this code, we will have a blank space at the beginning and end of our complete String. To solve this we need to trim those of.Alternatively, you could use a more complex regex that prevents the use of a trim in the end:
String test = "00 0 000 0000 0";
test = test.replaceAll("(?<!0)(0{2})(?!0)", "Y");
test = test.replaceAll("(?<!0)(0{1})(?!0)", "X");
System.out.println(test);
This code also prints the same result Y X 000 0000 X
How this one works:
(?<!0) Make sure that there are no extra 0 items before what we search. This is called a look behind(0{2}) Check for 2 occurrences of 0. Alternatively you could also put 00 here instead of specifying we expect a 0 twice(?!0) Make sure that there are no extra 0 items behind what we search. This is called a look aheadTwo approaches without regex.
Based on Arvind's ternary operator improvement and Andreas' approach (length).
Also avoiding the trailing space, one liner loop.
String str = "00 0 000 0000 0";
String[] pieces = str.split(" ");
str = "";
for (String s : pieces) {
str += (str.length()>0 ? " ":"") + (s.length()==1 ? "X" : (s.length()==2 ? "Y" : s));
}
//str => "Y X 000 0000 X"
Using equals:
String base = "00 0 000 0000 0";
String[] pieces = base.split(" ");
String rep="";
for (String s : pieces)
{
if (s.equals("0"))
rep+="X";
else if (s.equals("00"))
rep+="Y";
else
rep+=s;
rep+=" ";
}
//rep = rep.trim(); -> if need to cut off the last space
System.out.println(rep); // Y X 000 0000 X
Using the ternary (or conditional) operator, you can shorten the code further:
for (String s : pieces) {
rep += s.equals("0") ? "X" : (s.equals("00") ? "Y" : s);
rep += " ";
}
You can use regex, I think one of the easiest one would be the following
Replace \b0\b by X and also \b00\b by Y