use replaceAll and toLowerCase methods like this:
myString = myString.replaceAll(" ", "_").toLowerCase()
use replaceAll and toLowerCase methods like this:
myString = myString.replaceAll(" ", "_").toLowerCase()
This works for me:
itemname = itemname.replaceAll("\\s+", "_").toLowerCase();
replaceAll("\\s+", "_") replaces consecutive whitespaces with a single underscore.
"first topic".replaceAll("\\s+", "_") -> first_topic
"first topic".replaceAll(" ", "_") -> first__topic
java - Replace any number of consecutive underscores with a single space - Code Review Stack Exchange
java - Convert spaces to underscores - Stack Overflow
txt - how to replace spaces to underscores in java - Stack Overflow
How to convert all the spaces in an input to underscores
I think overall it is a nice implementation. It is clear and readeable. The use of constants is nice, makes it easy to refactor the code to make it more generic later on (e.g. replace with given character, not just space). Clear variable names (although I'd try and be consistent, and use current\previous, or cur/prev, most likely the former).
I have two remarks:
- I am not a fan of the new line at the start of a function, it is too much, and makes me lose my focus.
- I'd change the check
if (currentChar != prevChar)toif (prevChar != UNDERSCORE). Although we can know from the check above thatcurrentCharis an underscore, this makes it even more clear.
I don't really think you have what I'd see as duplication, but a possible other way to implement it would be to keep track of wether you are currently in a sequence of underscores. Example:
boolean previousWasUnderscore = false;
for (int i = 0; i < input.length(); i++) {
final char currentChar = input.charAt(i);
if (currentChar == UNDERSCORE) {
if (!previousWasUnderscore ) {
output.append(SPACE);
previousWasUnderscore = true;
}
} else {
output.append(currentChar);
previousWasUnderscore = false;
}
}
For generality, I would define a function that accepts the old and new delimiters as parameters, then overload it to make the underscore and space as defaults.
To avoid the special case for input.isEmpty(), I would define a boolean variable to indicate whether the previously encountered character was an underscore. You would also avoid having to re-inspect the previous character to see whether it was an underscore.
You can edit the string in place (using the same buffer for the input and output). It's slightly less cumbersome than calling the StringBuilder methods, in my opinion.
public class ReplaceUnderscore {
public static String replace(String input) {
return replace('_', ' ', input);
}
public static String replace(char oldDelim, char newDelim, String input) {
boolean wasOldDelim = false;
int o = 0;
char[] buf = input.toCharArray();
for (int i = 0; i < buf.length; i++) {
assert(o <= i);
if (buf[i] == oldDelim) {
if (wasOldDelim) { continue; }
wasOldDelim = true;
buf[o++] = newDelim;
} else {
wasOldDelim = false;
buf[o++] = buf[i];
}
}
return new String(buf, 0, o);
}
}
Hey, guys. Iโm a total newbie when it comes to regex and have no idea what Iโm looking at, so Iโm asking for your help. How can I replace spaces with underscores using a regex in EPLAN?
Example string: "This is a test" --> "This_is_a _test"
I also have an image of something else Iโve done where I removed '&E5/' from the string so that only "011" was left.
In EPLAN:
Where there are a Source Text and Output Text, one can put RegEx expressions.
Solution: