I have a string like this:
mysz = \"name=john age=13 year=2001\";
I want to remove the whitespaces in the string. I tried trim()
I am trying an aggregation answer where I test all ways of removing all whitespaces in a string. Each method is ran 1 million times and then then the average is taken. Note: Some compute will be used on summing up all the runs.
1st place from @jahir 's answer
2nd place
3rd place
4th place
public class RemoveAllWhitespaces {
public static String Regex(String text){
return text.replaceAll("\\s+", "");
}
public static String ForLoop(String text) {
for (int i = text.length() - 1; i >= 0; i--) {
if(Character.isWhitespace(text.codePointAt(i))) {
text = text.substring(0, i) + text.substring(i + 1);
}
}
return text;
}
public static String StringBuilder(String text){
StringBuilder builder = new StringBuilder(text);
for (int i = text.length() - 1; i >= 0; i--) {
if(Character.isWhitespace(text.codePointAt(i))) {
builder.deleteCharAt(i);
}
}
return builder.toString();
}
}
import org.junit.jupiter.api.Test;
import java.util.function.Function;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
public class RemoveAllWhitespacesTest {
private static final String longText = "123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222";
private static final String expected = "1231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc222";
private static final String shortText = "123 123 \t 1adc \n 222";
private static final String expectedShortText = "1231231adc222";
private static final int numberOfIterations = 1000000;
@Test
public void Regex_LongText(){
RunTest("Regex_LongText", text -> RemoveAllWhitespaces.Regex(text), longText, expected);
}
@Test
public void Regex_ShortText(){
RunTest("Regex_LongText", text -> RemoveAllWhitespaces.Regex(text), shortText, expectedShortText);
}
@Test
public void For_LongText(){
RunTest("For_LongText", text -> RemoveAllWhitespaces.ForLoop(text), longText, expected);
}
@Test
public void For_ShortText(){
RunTest("For_LongText", text -> RemoveAllWhitespaces.ForLoop(text), shortText, expectedShortText);
}
@Test
public void StringBuilder_LongText(){
RunTest("StringBuilder_LongText", text -> RemoveAllWhitespaces.StringBuilder(text), longText, expected);
}
@Test
public void StringBuilder_ShortText(){
RunTest("StringBuilder_ShortText", text -> RemoveAllWhitespaces.StringBuilder(text), shortText, expectedShortText);
}
private void RunTest(String testName, Function func, String input, String expected){
long startTime = System.currentTimeMillis();
IntStream.range(0, numberOfIterations)
.forEach(x -> assertEquals(expected, func.apply(input)));
double totalMilliseconds = (double)System.currentTimeMillis() - (double)startTime;
System.out.println(
String.format(
"%s: %s ms (%s ms)",
testName,
totalMilliseconds / (double)numberOfIterations,
totalMilliseconds
)
);
}
}