Search code examples
stringcharstringbuilder

Replacing a char in a string


I am trying to replace "2" with "C".

String numbers = "99929";
StringBuilder temp = new StringBuilder(numbers);
temp.setCharAt(3, "C");

I am getting an error stating

I ncompatible type, String cannot be converted to Char.

What does this mean? Sorry im new to this. Appreciate your kind help!


Solution

  • "C" (double quoted literal) is a String in Java but setCharAt accepts a char as the second parameter, try 'C' instead which is a char literal:

    temp.setCharAt(3, 'C');