Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:28:32+00:00 2026-06-14T14:28:32+00:00

Say I had the string foo1bar2 and I wanted to replace to perform the

  • 0

Say I had the string “foo1bar2” and I wanted to replace to perform the following replacements in parallel with an expected output of “bar1foo2”.

foo => bar
bar => foo

The string cannot be tokenized as the substrings might occur anywhere, any number of times.

A naive approach would to be to replace like this, however it would fail as the 2nd replacement would undo the first.

String output = input.replace("foo", "bar").replace("bar", "foo");
=> foo1foo2

or

String output = input.replace("bar", "foo").replace("foo", "bar");
=> bar1bar2

I’m not sure regex can help me here either? This isn’t homework by the way, just geeky interest. I’ve tried googling this but unsure how to describe the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:28:33+00:00Added an answer on June 14, 2026 at 2:28 pm

    Try first replacing “foo” with something else that won’t occur anywhere else in the String. Then replace “bar” with “foo” then replace the temporary replacement from step 1 with “bar”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I had the following string: var str = '(1 + foo + 3)
For example, lets say I had the following code: String s1 = foobar; s1
So lets say I had a string that was <html> <head><title>301 Moved Permanently</title><head> and
Say I had the following HTML: <h2>Heading <span></span></h2> <ul> <li><a href=# title=I am wanting
This seems stupid - must be me! Say I had a string filter[size] =
Say you had a timestamp function and then wanted to create a new function
Say I had a very simple struct in c# public struct foo{ public int
Say I had a String: Hello! I have to do all this: Convert string
Say I had a string that always had a two letter identifier such as
Let's say you had a string test = 'wow, hello, how, are, you, doing'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.