Re: replacing substrings in strings
From: James Curran (JamesCurran_at_mvps.org)
Date: 01/04/05
- Next message: MarkT: "RE: Declaring inherited class objects"
- Previous message: Richard Blewett [DevelopMentor]: "Re: C# properties: nothing more syntactic sugar for getters and setters"
- In reply to: Jim Lawton: "Re: replacing substrings in strings"
- Next in thread: Jon Shemitz: "Re: replacing substrings in strings"
- Messages sorted by: [ date ] [ thread ]
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
>
- Next message: MarkT: "RE: Declaring inherited class objects"
- Previous message: Richard Blewett [DevelopMentor]: "Re: C# properties: nothing more syntactic sugar for getters and setters"
- In reply to: Jim Lawton: "Re: replacing substrings in strings"
- Next in thread: Jon Shemitz: "Re: replacing substrings in strings"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|