aspose file tools*
The moose likes Beginning Java and the fly likes Replacing characters in a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replacing characters in a string?" Watch "Replacing characters in a string?" New topic
Author

Replacing characters in a string?

Tina Davis
Greenhorn

Joined: Sep 12, 2011
Posts: 5
I need a little help with an assignment I'm working on..
given a string entered into a text box by a user.. lets name this string text.

using a for loop to go through each char in the string.. I want to take that letter and replace it with a character 13 places after it.
for example:
A -> N
B-> O
C-> P

M-> Z
N-> A (There is no letter 13 places after N, so we “wrap around” to the beginning of the alphabet)

if the char happens to be a number or symbol.. I will simply leave it as.

there is no method in the string or character class that I can fin that can help me with this.
I do not want to use an Array or ArrayList.
an I obviously cant do a + 13.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

So what excatly causes problem?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4489
    
    8

Hi Tina. Welcome to The Ranch!

Tina Davis wrote:an I obviously cant do a + 13.


You sure about that? chars are actually integer types, so once you've got the letter into a char variable, you can just add numbers to it. It's a useful trick for things like this.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

excatly. You can do this adding just like this:
Tina Davis
Greenhorn

Joined: Sep 12, 2011
Posts: 5
Matthew Brown wrote:Hi Tina. Welcome to The Ranch!

Tina Davis wrote:an I obviously cant do a + 13.


You sure about that? chars are actually integer types, so once you've got the letter into a char variable, you can just add numbers to it. It's a useful trick for things like this.


yep. "type mismatch, cannot convert from int to char."
Tina Davis
Greenhorn

Joined: Sep 12, 2011
Posts: 5
Zandis Murāns wrote:excatly. You can do this adding just like this:


When I do it I get an error telling me I cannot convert from an int to a char.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4489
    
    8

You might need a cast to get it to allow the assignment (because a char + int is considered an int). Although try the += method, as that has a built-in implicit cast.
Tina Davis
Greenhorn

Joined: Sep 12, 2011
Posts: 5
the += worked...
however.. as I go through the string using a for loop one char at a time.. I want to determine if there is an int.. if so.. then I change nothing..
if the original letter is a capital.. then the new char should be capital.. and I want to test for whitespace as well..
so i know I need a few if statements...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Sounds like you have enough info to make a go at it. Be sure to use code tags when posting your solution.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tina Davis
Greenhorn

Joined: Sep 12, 2011
Posts: 5
actually... although the += 13 does work... it does not work CORRECTLY.
for one, if the char is the letter z. then the result ends up being ‡a weird symbol i cant paste here, and if its a capital Z then the result is lower case g when it should be capital M.

I know I need some "if" statements.
If the character is not a letter, leave the character unchanged
If the character is an uppercase letter, replace it with the uppercase letter that’s 13 positions after it in the alphabet, wrapping around to the beginning of the alphabet if necessary
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Please see the ASCII table. As you can see, if you add 13 to the char 'z', resulting char will be 122 + 13 = char at index #135.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Beware of +=; there are peculiarities about it because it involves both widening and narrowing conversion, so its behaviour is only reliable when used on ints and longs (and probably, but I am not certain) on floating-point numbers.
Whoever said "casting" was correct.
Don't use 122 for 'z'; use 0x7a. The sooner you learn hexadecimal arithmetic, the better. You can look up ASCII charts in all sorts of places, but Java™ doesn't use ASCII. It uses Unicode. You can find the values here and here. You will notice the values in the first link are identical to ASCII, however. If you add 'z' (0x7a) to 0x0d (13) you get 0x87, and you can look up on the second link what that means. If it isn't a printing character, you needn't expect it to appear on the command line. This html editor uses ‡ for (char)0x87. You can get it to appear in a Java™ String literal like this:
Are you sure you meant + 13 and not - 13?

Anyway, Strings are immutable, so you needn't expect to replace characters in them in the first place. You can get a copy by dumping the String into a char[] or putting it into a StringBuilder. You can use the % operator for wrapping round or even choosing between + 13 and -13, but I think you want to break this job into small bits.
  • Get a pencil and paper and a String like "Campbell Ritchie and Zandis Murāns are trying to help Tina Davis 89374943^%$&^^(%#']?"
  • Write it out as a char[]
  • Look at each char and its Unicode value.
  • Work out what you get with c % n
  • Work out what the best value for n above would be
  • Consider what modification to make to the char
  • Create a new String from the char[]
  • But don't do all those things. Do them one at a time, and print out the result after each stage.
    Also look in the Character class and see whether you are allowed to use its many useful methods.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Replacing characters in a string?