Search code examples
javaif-statementio

How to remove spaces before and after the comma in java


I have an input mismatch error.

used regular expression ("//s*,//s*",",") but it doesn't work.


Solution

  • You should use \\ instead of //

    String input = "a , b , c , d";
    String output = input.replaceAll("\\s*,\\s*", ",");
    System.out.println(output); // output: "a,b,c,d"