-1

I got a string x with a value. There, I want to replace a sequence of xx111159 as idno where xx can be anything but it must be replaced. Other chars remain the same.

Example: ab111159ab as idnoab and cd111159 as idno.

Is there a way for this to be done with Replace funct? I know a way that would need to check if the last chars is 111159 but that would cause to read the whole string one by one char...

1
  • Is this safe to assume that the string will always start with xx111159? Whats would you expect as a result if the string contains only 111159 i.e. it start with 111159 without any char in front of the sequence? Commented Dec 23, 2016 at 23:16

3 Answers 3

2

With a regular expression and String.replaceAll() you can accomplish your goal:

String result = x.replaceAll(".{2}111159", "idno");

Where . matches any character and {2} ensures there's two characters before 111159.

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

1 Comment

plus 1 because you explained the expression.
2

Use regex and replace

 str = str.replaceAll(".{2}111159","idno")

Comments

1

Use a regular expression:

x = x.replaceAll(".{2}111159", "idno");

Comments

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.