Search code examples
javastringreplaceall

Java replaceALL for string


I have a string:

100-200-300-400

i want replace the dash to "," and add single quote so it become:

 '100','200','300','400'

My current code only able to replace "-" to "," ,How can i plus the single quote?

String str1 = "100-200-300-400";      
split = str1 .replaceAll("-", ",");

if (split.endsWith(",")) 
{
   split = split.substring(0, split.length()-1);
}

Solution

  • You can use

    split = str1 .replaceAll("-", "','");
    split = "'" + split + "'";