Re: replacing substrings in strings

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: James Curran (JamesCurran_at_mvps.org)
Date: 01/04/05


Date: Tue, 4 Jan 2005 13:06:34 -0500

This seems a bit more flexible (you can just use a string for the search
characters), and a bit faster (3 sec vs 4.5 sec --- after 1,000,000
repetitions)

static private string Test2()
{
  string value = "the quick ==brown== fox jumps ==over== the lazy ==dog==";
  // The search string.
 string searchString = "==";

 // Replace with ??
 bool replaceWithQuestionMarks = true;

 // The StringBuilder.
 StringBuilder result = new StringBuilder(value.Length);

 int start = 0;
   int index = 0;
 while ( (index = value.IndexOf(searchString, start)) > -1)
 {
         // Append substring value[start...index-1]
       result.Append(value, start, index-start);

         // Add the string.
         result.Append((replaceWithQuestionMarks ? "??" : "!!"));

         // Flip the bit.
         replaceWithQuestionMarks = !replaceWithQuestionMarks;

         // Add 1 to the index.
   start = index + searchString.Length;
 }
 return result.ToString();
}

-- 
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com       Work: www.njtheater.com
Blog: www.honestillusion.com  Day Job: www.partsearch.com
"Jim Lawton" <ucan@use.your.initiative> wrote in message
news:7gclt09v301na0p42dlem0u7lnbp5bebf6@4ax.com...
> On Tue, 4 Jan 2005 09:21:04 -0500, "Nicholas Paldino [.NET/C# MVP]"
> <mvp@spam.guard.caspershouse.com> wrote:
>
> >Jim,
> >
> >    You could use regular expressions, but I think that it would be
> >complicated to do the replace for every other item.
> >
> >    Personally, I would cycle through character by character (IndexOf is
> >going to do the same thing, and if you use it, you will have to call it
> >multiple times, better to just cycle through the characters once).
> >
> >    Basically, I'd do this:
> >
> >// The search string.
> >string searchString = "==";
> >
> >// The substring.
> >string subString = null;
> >
> >// Replace with ??
> >bool replaceWithQuestionMarks = true;
> >
> >// The StringBuilder.
> >StringBuilder result = new StringBuilder(value.Length);
> >
> >// Cycle through all of the characters.
> >// "value" has the value to search.
> >for (int index = 0; index < value.Length - 1; ++index)
> >{
> >    // If the characters are == then continue.
> >    if (value[index] == '=' && value[index + 1] == '=')
> >    {
> >        // Add the string.
> >        result.Append((replaceWithQuestionMarks ? "??" : "!!"));
> >
> >        // Flip the bit.
> >        replaceWithQuestionMarks = !replaceWithQuestionMarks;
> >
> >        // Add 1 to the index.
> >        index++;
> >    }
> >    else
> >    {
> >        // Just append the character.
> >        result.Append(value[index]);
> >    }
> >}
> >
> >    Hope this helps.
>
> Thanks for that Nicholas - I was hoping not to have to code such a
primitive
> solution - thought there might be something built-in which would return
the
> index of the latest replacement ...
>
> never mind, I didn't get where I am today without coding round stuff ;-)
>
> thanks again,
> Jim
>


Relevant Pages

  • Re: A truely BIJECTIVE BWT is here!
    ... I left the old BWT stuff since its the way you compare BWTS ... FOOBAR2000 <-- orignal input string, ... Zero as seprate cycle and it actually sorts first than current version ... character sequences. ...
    (comp.compression)
  • Re: ReplacerStream
    ... string, do a replace on that string and create a stream again to be ... If those are problems, and you are looking just for a single string, it seems to me that you could just read the stream one character at a time, checking to see if it matches the current character in your search string. ...
    (microsoft.public.dotnet.framework)
  • Re: A truely BIJECTIVE BWT is here!
    ... If you look at the BWT ... rotation of a given input to the same output string. ... shorter cycle that doesn't include all characters. ... character sequences. ...
    (comp.compression)
  • Re: Execution of a full-text operation failed. A clause of the query contained only ignored words.
    ... Paul's search string from his initial posting including a long string of ... The above can be converted to a contains clause using the pubs table ... this is less about searching on noise words in the noise word ... Why would someone want to parse a search string to ...
    (microsoft.public.sqlserver.fulltext)
  • Re: two interesting data structure/algorithm questions
    ... for an arbitrary string and return the page numbers where this string ... In other words, for now, forget about the search string being able ... and so on in the inverted index instead of just the word "this". ...
    (comp.programming)