1

I have a String called

String s = "Constitución Garantía";

I want to convert it to Constitución garantía.

This is a Spanish keyword. How can I convert it?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
user9130953
  • 449
  • 2
  • 13
  • 6
    looks like you have encoding problems, solve that in stead of trying to change the string (and remember, if this comes from logging, the logging an also be the problem and not the code) – chillworld Jun 27 '18 at 12:16
  • 2
    Your input ``String s`` is already broken. – f1sh Jun 27 '18 at 12:16
  • 1
    Its an encoding issue, have a look at -> https://stackoverflow.com/questions/5729806/encode-string-to-utf-8 – bko Jun 27 '18 at 12:20

1 Answers1

3

What you have described is an XY problem. It's the encoding issue and there might appear more of the characters that need to be replaced. Instead of replacing them one by one, you need to encode the whole String to UTF-8.

String s = "Constitución Garantía"; 
byte[] ptext = s.getBytes(StandardCharsets.ISO_8859_1); 
String string = new String(ptext, StandardCharsets.UTF_8);      
System.out.println(string);                                 // Constitución Garantía

Consider fixing the encoding of a source where the string comes from before you actually start to work with it.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183