Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

I want to do String.replaceAll(regex, regex) instead of (regex, String) in Java

Example:

input = "10N10N";
input = input.replaceAll("1|N", "N|1"); // syntax not correct

expected output: N01N01

What I'm asking is for a single-lined iterator that replaces all "1" with "N" and all "N" with "1".

Answer*

Draft saved
Draft discarded
Cancel
2
  • Could you explain what you mean by "more secure solution?" Commented Feb 3, 2018 at 18:18
  • 1
    @Joey Grant The first solution need a 3rd character, I used - but what if the input can have a hyphen before? for example 10N-10N in this case the 3rd replace will replace also this hyphen, but the second solution don't need that 3rd character, It loop over all character and check if it is equal to N then return 1 else if N return it with 1 and in the end collect all the result this is what is mean by "more secure solution" hope you get it Commented Feb 3, 2018 at 18:23

lang-java

AltStyle によって変換されたページ (->オリジナル) /