1

Basically, what I want to achieve is replacing each letter in a string with another letter or a combination of letters. First, I tried

String codedText = text.replace("a","b")
                       .replace("b","a")
                       .replace("cd", "c");

and so on, but the problem is the following. Using this one, it will replace in a infinite loop, also here is another problem. As you can see on the third replace part, I'm searching for a group of letters to replace with one, also sometime I'm using .replace("c", "cd"), which means that there might be an output of 2 letters, in case of using .replace, it will try to replace the "d" letter aswell.

So, to make things a little bit clear, I need something that would basically split the text into array, each containing 1 letter, convert everything and after that make from array a text and output it. I hope I explained it pretty good, because it's really pretty hard to explain that as a newbie in java.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    I voted to close your question as duplicate but not because of accepted solution there, but this answer: http://stackoverflow.com/a/21419100. If solution described there is not clear (or you are not familiar with regex) let me know and I will try to clarify it. – Pshemo Oct 14 '16 at 18:30
  • Hy. I looked into the answers there before submitting. I found the hashmap pretty usefull, but my words are followed by other, and I don't think I can do anything in regex that would make java to replace all values only once. – Cojocari Cristian Oct 14 '16 at 18:55
  • I am not sure what you mean by "but my words are followed by other" (or at least why is it a problem) and "that would make java to replace all values only once". Maybe example containing some text, replacements you want to make, and expected result would be helpful. – Pshemo Oct 14 '16 at 18:59
  • For now I don't see any problems with what you already posted and mentioned solution from duplicate question. But maybe you are willing to use outside libraries like one mentioned here: http://stackoverflow.com/a/7544706 – Pshemo Oct 14 '16 at 19:01
  • Idea: build a regexp matching any string that should be replaced (`"a|b|cd"`). Iterate over matches and for each match replace it with the proper replacement. This will make sure that all replacements are made exactly once and replacements are not made within replacements. – Ole V.V. Oct 14 '16 at 19:01
  • 1
    Oh, only thing I would be aware of while trying to build regex like `a|b|c|cd` is that `cd` would need to be placed before `c` since `|` in regex is evaluated from left to right, so most specific parts should be placed before more general ones. So instead of `a|b|c|cd` we would need `a|b|cd|c`. – Pshemo Oct 14 '16 at 19:03
  • Here is a simple text. "This is a text to get coded ASAP ". After "coding" it should look like "X21Z 1Z 10 X3RX XE 73X 4E939 10Z10C". After decoding, it should go back to "This is a text to get coded asap". Basically, each letter has another letter or a number, or a combination of numbers/numbers + latters. This is why I said it might be a problem using regex, maybe I don't know enought about Regex. – Cojocari Cristian Oct 14 '16 at 19:08
  • If you want regex which should find series of digits and/or letters you can represent it as `[0-9A-Z]+`. Now each time you will call `Matcher#find` you will get single words like `X21Z`, then with next `find` `1Z` and so on. – Pshemo Oct 14 '16 at 19:18
  • Only problem now is that you need to apply some decoding method to these *words* to get their original values. – Pshemo Oct 14 '16 at 19:19
  • Basically, on decoding I though I'll just get from the bottom to the beginning. Like from combinations to normals. I'll try to serach StringUtils for Android Studio, it might be what I need – Cojocari Cristian Oct 14 '16 at 19:22

0 Answers0