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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:18:04+00:00 2026-06-10T11:18:04+00:00

Anyone know how I would find & replace text in a string? Basically I

  • 0

Anyone know how I would find & replace text in a string? Basically I have two strings:

string firstS = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABQODxIPDRQSERIXFhQYHzMhHxwcHz8tLyUzSkFOTUlBSEZSXHZkUldvWEZIZoxob3p9hIWET2ORm4+AmnaBhH//2wBDARYXFx8bHzwhITx/VEhUf39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f3//";

string secondS = "abcdefg2wBDABQODxIPDRQSERIXFh/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/abcdefg";

I want to search firstS to see if it contains any sequence of characters that’s in secondS and then replace it. It also needs to be replaced with the number of replaced characters in squared brackets:

[NUMBER-OF-CHARACTERS-REPLACED]

For example, because firstS and secondS both contain “2wBDABQODxIPDRQSERIXFh” and “/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/” they would need to be replaced. So then firstS becomes:

string firstS = "/9j/4AAQSkZJRgABAQEAYABgAAD/[22]QYHzMhHxwcHz8tLyUzSkFOTUlBSEZSXHZkUldvWEZIZoxob3p9hIWET2ORm4+AmnaBhH//2wBDARYXFx8bHzwhITx/VEhUf39[61]f3//";

Hope that makes sense. I think I could do this with Regex, but I don’t like the inefficiency of it. Does anyone know of another, faster way?

  • 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-10T11:18:05+00:00Added an answer on June 10, 2026 at 11:18 am

    Does anyone know of another, faster way?

    Yes, this problem actually has a proper name. It is called the Longest Common Substring, and it has a reasonably fast solution.

    Here is an implementation on ideone. It finds and replaces all common substrings of ten characters or longer.

    // This comes straight from Wikipedia article linked above:
    private static string FindLcs(string s, string t) {
        var L = new int[s.Length, t.Length];
        var z = 0;
        var ret = new StringBuilder();
        for (var i = 0 ; i != s.Length ; i++) {
            for (var j = 0 ; j != t.Length ; j++) {
                if (s[i] == t[j]) {
                    if (i == 0 || j == 0) {
                        L[i,j] = 1;
                    } else {
                        L[i,j] = L[i-1,j-1] + 1;
                    }
                    if (L[i,j] > z) {
                        z = L[i,j];
                        ret = new StringBuilder();
                    }
                    if (L[i,j] == z) {
                        ret.Append(s.Substring( i-z+1, z));
                    }
                } else {
                    L[i,j]=0;
                }
            }
        }
        return ret.ToString();
    }
    // With the LCS in hand, building the answer is easy
    public static string CutLcs(string s, string t) {
        for (;;) {
            var lcs = FindLcs(s, t);
            if (lcs.Length < 10) break;
            s = s.Replace(lcs, string.Format("[{0}]", lcs.Length));
        }
        return s;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string: <li>&File</li> . What I would like to do is analyse
Would anyone know why the bmlabel is not updated? and the log score shows
Would anyone know how to test for the appearance of a Toast message on
Does anyone know how I would go about changing (transforming) an image based on
I am using double[] instead of NSArray. Would anyone know how to encode it
Does anyone know a script which would wrap old Products namespace style Plone add-on
Does anyone know of a library that would allow me to manipulate Flex DOM
Does anyone know the display formatter that I would need to add to the
Does anyone know why a library initialized within dlopen() would initialize a static variable
Does anyone know of a C# spell check library that would underline misspelled works

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.