0

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

6
  • Are you trying to perform Hex addition? Commented Feb 25, 2012 at 0:06
  • 2
    Exactly what are you trying to achieve.? and what do you mean by string1+string2=result. Commented Feb 25, 2012 at 0:07
  • I am trying to add two strings, first they are String add1=AEFG, String add2=SDFD, String result=WECS. By changing letters to number will become add1=3849, add2=3234, result=29830. If add1+add2=result then return true, else keep replacing. But I just need to know how to replace a single char in a string. Thanks. Commented Feb 25, 2012 at 0:18
  • @Bartg: than see Anton's answer. Commented Feb 25, 2012 at 0:23
  • @RanRag This is more indept than just search and replace. I think what Bart want is a Alpha numeric addition. And My only suggestion is, converts the alpha into it's ascii value and add them. But you need to manager decimal places and carry overs, such as What does it means when an X+Y==1W? Commented Feb 25, 2012 at 0:34

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Seriously, That was what you are looking for? a replaceAll?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.