What is the regular expression for replaceAll() function to replace "N/A" with "0" ?
input : N/A
output : 0
Why use a regular expression at all? If you don't need a pattern, just use replace:
String output = input.replace("N/A", "0");
replaceAll at all.You can try a faster code. If the string contains only N/A:
return str.equals("N/A") ? "0" : str;
if string contains multiple N/A:
return join(string.split("N/A"), "0")
+ (string.endsWith("N/A") ? "0" : "");
where join() is method:
private String join(String[] split, String string) {
StringBuffer s = new StringBuffer();
boolean isNotFirst = false;
for (String str : split) {
if (isNotFirst) {
s.append(string);
} else {
isNotFirst = true;
}
s.append(str);
}
return s.toString();
}
it is twice as fast
split() plus join() is faster than replace() or regex?