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 9244069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:56:25+00:00 2026-06-18T08:56:25+00:00

Why does line2 replaces only alternating half of occurrences? Dim line1 As String =

  • 0

Why does line2 replaces only alternating half of occurrences?

    Dim line1 As String = "AAA|BBB|CCC|CCC|CCC|CCC|EEE|FFF"
    Dim line2 As String = "AAA|BBB|CCC|CCC|CCC|CCC|EEE|FFF"
    Dim line3 As String = "AAA|BBB|CCC|CCC|CCC|CCC|EEE|FFF"

    line1 = line1.Replace("CCC", "")
    line2 = line2.Replace("|CCC|", "||")
    line3 = line3.Replace("CCC|", "|")

Result:

line1 = "AAA|BBB|||||EEE|FFF" -- OK, but fails when element is "..|ZZZCCCZZZ|.."
line2 = "AAA|BBB||CCC||CCC|EEE|FFF" -- Not OK
line3 = "AAA|BBB|||||EEE|FFF" -- OK, but fails similar to Line1 edge-case for "..|ZZZCCC|.."

I have tried using RegEx, but get similar results.

Is there really no better way than this, below?

Do While line1.Contains("|CCC|")
    line1 = line1.Replace("|CCC|", "||")
Loop
  • 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-18T08:56:26+00:00Added an answer on June 18, 2026 at 8:56 am

    Once it finds the first token, it starts looking for the next one after that token. So it finds |CCC|, replaces it, then continues on and the first thing it sees is CCC| which doesn’t match. It doesn’t pre-scan the string looking for tokens to replace.

    Consider it like this:

    Given AAA|BBB|CCC|CCC|CCC|CCC|EEE|FFF

    It runs to AAA|BBB|CCC| HOLD IT |CCC| was found, let’s start building our string:

    AAA|BBB + || (our replacement)

    Now let’s move on, we now have CCC|CCC|CCC|EEE|FFF left to work with.

    It runs to CCC|CCC| HOLD IT |CCC| was found, let’s continue adding to our string:

    AAA|BBB||CCC + || (our replacement)

    Now let’s move on, we now have CCC|CCC|EEE|FFF and so on and so on.

    EDIT: Considering the entry on MSDN describing the return value:

    A string that is equivalent to the current string except that all
    instances of oldValue are replaced with newValue.

    One could read that as what you expect that it pre-scans the string and finds all matches. I don’t see anything in the MSDN doc that describes this corner case. Perhaps this is something that should be added to the MSDN doc.

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

Sidebar

Related Questions

Got a problem where preg_replace only replaces the first match it finds then jumps
Possible Duplicate: Why does javascript replace only first instance when using replace? How do
Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents,
Why does Memo.Lines use the abstract class TStrings ? Why doesn't it use TStringList
What does this line of code will do. pcm->card->number , pcm->device both are int
The commandlink link2 does not have work after an ajax call is made to
What does this line (x = n & 5;) mean in the code below?
I am looking for a reusable code snippet that does command line argument validation
when including libraries c, the line does not end with a semicolon, while other
The following code doesn't work (of course), because the marked line does not compile:

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.