Bart g
Bart g

Reputation: 585

replace a character in a String

I would like to replace a char in a string. My idea was this:

public Puzzle replace(String letter, int digit)
{
   String str = letter;
   String d = ""+digit;
   String nStr = str.replace(letter,d);
   Puzzle newPuzzle = new Puzzle(nStr, d, str);
   return newPuzzle;
  // ...
}

but the replacing happens only if the "String str = letter"(letter) but it should be something like "String str = string"(string), Example => A2B+1A1=AAC would become 32B+131=33C this would be the outcome if I replace the letter 'A' by '3' in the string, and this would repeat until all the letters change to an int and the sum of string1+string2=result. Any help in appreciated. Thank you

Upvotes: 0

Views: 376

Answers (1)

Anton
Anton

Reputation: 1515

I am not quite sure what you mean, but this part: "Example => A2B+1A1=AAC would become 32B+131=33C this would be the outcome if I replace the letter 'A' by '3' in the string"

Can be achieved by using replaceAll().

Strin nStr = str.replaceAll(letter,d);

Hope that helps to some extent.

Upvotes: 3

Related Questions