Creating a Method to Replace Characters in a String

Asked

Viewed 252 times

0

Can someone help me create a method that replaces the Characters of a String? For example,

String nova = antiga.replace('a','1'); 

This only replaces a letter.

I wanted to create a method for all other letters of the alphabet that I could use whenever I wanted to replace the characters of a certain String.

  • Are you using data input through the console ? Or have some visual screen ?

  • @Matheus as well as visual screen?

  • 2

    Knowing if it is a graphical interface or console is irrelevant to doubt, the replaceall mentioned below does exactly what you want.

1 answer

3


The method itself replace has this purpose. Note that there is an overload that receives two CharSequence's as a parameter.

I mean, you can do it like this

String nova = antiga.replace("alguma coisa", "outra coisa"); 

See working on repl.it.

Browser other questions tagged

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