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

  • Home
  • SEARCH
  • 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 6767277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:55:54+00:00 2026-05-26T14:55:54+00:00

I am having two string builders in which one holds the complete data and

  • 0

I am having two string builders in which one holds the complete data and the other holds the data that should be replaced but I am unable to replace the content.

This is the code i have written

StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();

sb1.AppendLine("1111111111");
sb1.AppendLine("555555555");
sb1.AppendLine("666666660");
sb1.AppendLine("6666666601");
sb1.AppendLine("6666666602");
sb1.AppendLine("6666666603");
sb1.AppendLine("888888888");
sb1.AppendLine("99999999");
sb1.AppendLine("555555555");
sb1.AppendLine("666666661");
sb1.AppendLine("666666662");
sb1.AppendLine("666666663");
sb1.AppendLine("666666664");
sb1.AppendLine("888888888");
sb1.AppendLine("99999999");

sb2.AppendLine("6666666601");
sb2.AppendLine("666666661");

sb1 = sb1.Replace(sb2.ToString().TrimEnd(charRemove), "");
sb1 = sb1.Replace(sb2.ToString(), ""); // this also i tried

I would like to remove sb2 content

  • 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-05-26T14:55:55+00:00Added an answer on May 26, 2026 at 2:55 pm

    You mustn’t use StringBuilder for sb2.

    To make it short, you are telling .NET to replace 6666666601666666661 in sb1.

    Try:

    List<string> list2 = new List<string>();
    list2.Append("6666666601")
    list2.Append("666666661")
    
    foreach (var str in list2)
    {
        sb1.Replace(str + "\r\n", ""); // the \r\n are because you used `AppendLine`
    }
    

    (and I’m not even sure you want to use a StringBuilder for sb1. StringBuilder is used to “compose” strings, not to “collect” them)

    Note that this is still wrong wrong wrong!

    You would be much better to begin the sb1 with an empty line, like this:

    sb1.AppendLine("");
    

    so that you can Replace like this:

    sb1.Replace("\r\n" + str + "\r\n", "\r\n"); // the \r\n are because you used `AppendLine`
    

    in this way, 99 won’t mach 1999, because 1999 will be \r\n1999\r\n while 99 will be \r\n99\r\n (we are adding a blank line at the beginning of the StringBuilder so that even the first element is preceeded by a \r\n. We are replaceing with \r\n because we are matching TWO \r\n, one before and one later, but one of the two must be preserved.)

    I’ll add that perhaps you simply exchanged StringBuilder for a collection of strings, and perhaps you wanted to do this:

    List<string> str1 = new List<string>();
    str1.Add("555555555");
    str1.Add("666666660");
    str1.Add("6666666601");
    str1.Add("666666661");
    
    List<string> str2 = new List<string>();
    str2.Add("6666666601");
    str2.Add("666666661");
    
    str1.RemoveAll(p => str2.Contains(p));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having two html files. One file should include the source of other
No one said that OrderedDictionary is having two copies of elements, one in a
Having two projects: one is a class library (a .dll assembly) and the other
Having the following classes, in two different assemblies: class Member { public string Label
We are having two load balancing server. In that we have hosted a asp.net
How do I go about having two view models communicate with one another using
i have two sheets having data like this sheet1 : **A** **B** **C** 752
I have a string variable having data 4096;jpg,bmp or 2048;flv,mpg I want to split
What would be the best C# data structure for using one key, and having
Suppose I am having two Strings as follows : String name = EXAMPLE_MODEL_1; String

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.