• Post Reply Bookmark Topic Watch Topic
  • New Topic

replace string characters with characters from another string  RSS feed

 
steven franklin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking to use a string index to replace characters in another string.

Basically,

str1 = "SOME SENTENCE"
str2 = "ZYXWVUTSRQPONMLKJIHGFEDCBA"

I want to:

read str1 and:

      for every 'A' found, replace with string2 (index 0)
      for every 'B' found, replace with string2 (index  1)
and so on until
      for every 'Z' found, replace with string2 (index  25)

return final result to str3

I want my end result to be:

str3 = "HLNV HVMVGEMXV"

I've found several ways to replace a character with another character, like replaceAt(), how can I do this while letting a string index dictate what the replacement will be?
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to the string API, what you need is in there.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to use the StringBuilder class, which is mutable. See the method setCharAt().

StringBuilder API doc
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!