Give replace to the chars of a string

Asked

Viewed 94 times

2

Well, the title of the topic says it all.

Like, I got the String a = "Banana é uma ótima fruta.", how do I give replace in the chars of the word "Banana" ?

I wanted the replace to stay, "****** é uma ótima fruta.".

  • can explain better what you really want? is that with information you can not understand :)

  • The first reply from this link can help you: https://stackoverflow.com/questions/16702357/how-to-replace-a-substring-of-a-string

2 answers

3

Try this:

public String mask(String template, String toMask) {
    int tamanho = toMask.length();
    StringBuilder replacement = new StringBuilder(tamanho);
    for (int t = 0; t < tamanho; t++) {
        replacement.add('*');
    }
    return template.replace(toMask, replacement);
}

And you wear it like this:

String substituido = mask("Banana é uma ótima fruta.", "Banana");
  • 1

    @Igorventurelli Very well noted. Thank you.

0

String a = "Banana é uma ótima fruta";

a = a.replace("Banana","******");

a // "****** é uma ótima fruta"

Use the method replace string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.