1

Just curious as to how I would add a letter or character to a pre-existing string.

Here is what I'm trying to do,

I'm taking a key and its cipher text and subtracting their values (stored in arraylist) to find the plaintext value (I get an int, and then use that to find the String value in the arraylist) and then continue to do that for each letter in the two strings(Cipher and Key).

Is this possible, or would I have to use a substring or something along those lines.

Any suggestions on how to do this, or the answer to my initial question is greatly appreciated!

Thanks,

Sully Brooks
  • 425
  • 4
  • 8
  • 21
  • 1
    You can't change an actual `String` object; but you can take a variable that references a `String` object, and make it reference a different one. Is that what you require? – Dawood ibn Kareem Jan 05 '14 at 05:37
  • I'm going to edit the question quickly and add more detail to it. One moment :P – Sully Brooks Jan 05 '14 at 05:38
  • 1
    I am still wondering how you are using `ArrayList` to store key value. Anyway you can use `StringBuilder`. – Helios Jan 05 '14 at 05:45
  • 1
    I am not getting this part `I get an int, and then use that to find the String value in the arraylist` ? – Bhavik Shah Jan 05 '14 at 05:47
  • @Nik well,first off, the "Key" is any bunch of letters, Ex: "ADIFG". What I did was add an array of letters(A-Z) to the ArrayList, then simply using their index in the ArrayList, I can an int, to use to subtract from the corresponding cipher keys value(of the current character we are examining) and then using THAT number, again,to get the letter stored at that index in the ArrayList. Hope that makes sense :P – Sully Brooks Jan 05 '14 at 05:49
  • @BhavikShah Read my above comment, its my best explanation for it quickly. – Sully Brooks Jan 05 '14 at 05:51
  • So you have 2 keys("ADIFG" and an cipher key), you map the key("ADIFG") to the value in arraylist, and then use cipher key to map same value in the arraylist again...if the 2 values are equal the test pass other wise fail...right? – Bhavik Shah Jan 05 '14 at 05:56
  • @SullyBrooks I guess I got it. I took it as conventional `key-value` thing. My bad. – Helios Jan 05 '14 at 05:57
  • @Nik Yes, I would have used a Map or TreeMap if that were the case, but I figured this would work equally as well, and a bit less work. – Sully Brooks Jan 05 '14 at 06:00
  • @BhavikShah Okay, so here is what the program actually DOES. I take in a text file that has a list of Values. The order of the values of the list go like so CIPHERTEXT (New Line) KEY. Then what the program does is, if they key is shorter than the ciphertext, then adjust that to fit the length(where I actually used a StringBuilder for that method). Then, once that is adjusted, a new method takes those values and tries to decipher them. It does this by subtracting the value of EACH character of the key (A=1,B=2,C=3...Z=26) from its corresponding character in the Cipher text. Will continue.. – Sully Brooks Jan 05 '14 at 06:04
  • @BhavikShah Conitnued, Then, with that remainder value, I get the letter (value) of the index(remainder) in the ArrayList. That then, is the Plaintext value. Then it moves on to the next Letter in the ciphertext/key pair. Somewhat understand? Sorry for the poor explanation if not. – Sully Brooks Jan 05 '14 at 06:05

4 Answers4

8

Did you look into

StringBuilder#append()

Adding characters to String Object is not recommended since String is immutable which will create a new object everytime (if the string you are creating is not already in String pool)

Update :

If you are looking for performance too, (and by any change there are a lot of string comparisons in your program) here is my suggestion:

use strRef1 == strRef2 instead of strRef1.equals(strRef2)

Since, String is immutable and the concept of String pool the references of Strings will always be equal so if

strRef1.equals(strRef2) returns true strRef1 == strRef2 should always return true.

Exception : this would not work if you create a new string explicitly like

strRef2 = new String();

Community
  • 1
  • 1
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
5

Since String is final (designed as immutable) you need to create every time new Strings. For repeated times I recommend to use StringBuilder and then (after all char additions) convert it to String via toString()-method.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • Gaah! How didn't I think of that! Thanks for the quick reminder! Accepting answer as soon as I am permitted to! – Sully Brooks Jan 05 '14 at 05:41
  • 2
    Please also keep in mind that `StringBuilder#append()` is much more efficient than using the + operator for string-concatenation. – Meno Hochschild Jan 05 '14 at 05:42
  • I realize that. As embarrassing as this may sound, I actually am using a StringBuilder in the current program I am writing....maybe its time I get some sleep haha – Sully Brooks Jan 05 '14 at 05:44
  • 3
    Strings being `final` has nothing to do with that. It's because they are *immutable* that you want a `StringBuilder`. – Jeroen Vannevel Jan 05 '14 at 05:50
  • 1
    @JeroenVannevel thanks for the hint, implicitly I have meant this, but the term "immutable" is much more precise. – Meno Hochschild Jan 05 '14 at 05:52
4

You could try

string += newChar;

where string is the initial String object and newChar is the string or character you want to append to the original string.

NOTE: This is not as efficient as using StringBuilder.append(...). If efficiency matters to you, you're better off with the other method. This, however, would suffice for very basic use cases.

Nathan Walters
  • 4,116
  • 4
  • 23
  • 37
4

Since strings are immutable, use StringBuilder so that you don't create a new String every time you want your String to include an additional letter

Jack
  • 370
  • 2
  • 4