Check and change the first 3 characters of a string?

0

For example, a string with telephone +5531984892883. I would like to check if the first 3 characters are equal to +55. If they are equal to +55, replace them with 0 (zero). How can I do this?

    
asked by anonymous 27.06.2017 / 18:49

1 answer

3

Use the replace :

final String telefone = "+5531984892883";
final String outroTelefone = "31984892883";

System.out.println("Telefone :" + telefone);
System.out.println("Telefone sem o codigo :" + telefone.replace("+55", "0"));
System.out.println();

System.out.println("Outro telefone :" + outroTelefone);
System.out.println("Outro telefone sem o codigo :" + outroTelefone.replace("+55", "0"));

Output:

Telefone :+5531984892883
Telefone sem o codigo :031984892883

Outro telefone :31984892883
Outro telefone sem o codigo :31984892883

Example online here .

    
27.06.2017 / 18:53