Can anyone point me in the right direction for finding and replacing multiple letters at the same time?
For example: Tommie becomes Thomas.
Thanks for your help in advance.
Find and replace multiple letters
Started by Renee55, Sep 20 2010 04:27 AM
5 replies to this topic
#1
Posted 20 September 2010 - 04:27 AM
|
|
|
#2
Posted 20 September 2010 - 06:26 AM
There are a few ways to do it; it really depends on exactly what the program is supposed to do.
Is it a program to detect nicknames and replace them with full names? If we have mroe details about exactly what you're doing, we may be able to offer better help :)
As for your example, you'd pretty much have to split the 'T' aff from 'Tommie' using the substring method and then add 'homas' to the original string. It doesn't seem very efficient.
Is it a program to detect nicknames and replace them with full names? If we have mroe details about exactly what you're doing, we may be able to offer better help :)
As for your example, you'd pretty much have to split the 'T' aff from 'Tommie' using the substring method and then add 'homas' to the original string. It doesn't seem very efficient.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#3
Posted 20 September 2010 - 07:07 AM
This will probably be a better example of what I'm trying to do. How can I change "hello" to "xatty" or "hello" to "cikko" or "hello" to any combination like a code.
Hello becomes xatty.
Hello becomes cikko.
Let me know if you need more info. Thanks!
Hello becomes xatty.
Hello becomes cikko.
Let me know if you need more info. Thanks!
#4
Posted 20 September 2010 - 07:13 AM
I don't really think that it's the letter replacement you want here because it is impossible to replace letters in nick-name "Tommie" to letters in name "Thomas" unless you have some algorithm that generates a new set of replacement letters after each replacement, because you see if you think of a String as an array of chars you have 0-5 indexes. so you see that if you go by simple letter replacement than we see that in the word Tommie index 2 and 3 have the same values ergo the word after letter replacement will also have same values at index 2 and 3. So you might just as well find and replace the whole String, which is actually pretty easy. Let's say you have a String text with a bunch of text in it... and the replacement in your example is done with only one method call:
text.replaceall("Tommie", "Thomas");
this will literally replace every part of the string that says "Tommie" with "Thomas".
text.replaceall("Tommie", "Thomas");
this will literally replace every part of the string that says "Tommie" with "Thomas".
#5
Posted 20 September 2010 - 07:21 AM
ok from your second example I see that it's the letter replacement you wanted... here I can think of two methods... one is make a replacement of all letters with anything you want with Switch-case or you can make a method that performs so called caesars encryption where you simply shift letters by a constant, by performing character mathematics. say the constant is 3 then you add 3 to every character so "hello" becomes "khoor".
#6
Posted 20 September 2010 - 08:15 AM
If you're doing encryption, then you'd bassically read the plaintext into a string, and then perform operations to build a second string based on the plaintext string (e.g. the plaintext would be 'hello', which you would use to build up a second string e.g. 'xatty', which would be the ciphertext)
So, basically, you'd read the string letter by letter using a for-loop, and use the charAt() method to identify the character at each location and from that, put the relevant new letter into the ciphertext string.
So, you don't change the original string; you simply build up a new string based on the input and the method you're using to 'replace' the letters (the substitution algorithm).
Looking at your desired output against your input, it seems you're just replacing letters according to an arbitrary designation? (e.g. all 'h' becomes 'x', all 'e' becomes 'a', all 'l' becomes t', etc...)? In which case I think that the Switch-Case method Roman suggested would be the best solution, unless you want a more complicated method to encipher the data?
So, basically, you'd read the string letter by letter using a for-loop, and use the charAt() method to identify the character at each location and from that, put the relevant new letter into the ciphertext string.
So, you don't change the original string; you simply build up a new string based on the input and the method you're using to 'replace' the letters (the substitution algorithm).
Looking at your desired output against your input, it seems you're just replacing letters according to an arbitrary designation? (e.g. all 'h' becomes 'x', all 'e' becomes 'a', all 'l' becomes t', etc...)? In which case I think that the Switch-Case method Roman suggested would be the best solution, unless you want a more complicated method to encipher the data?
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)


Sign In
Create Account

Back to top









