replacing substrings in strings

  • Thread starter Thread starter Jim Lawton
  • Start date Start date
J

Jim Lawton

Hello group,

I'm not very experienced in C#, but I can't find any example of this (common)
programming problem.

I have a string which contains a repeated substring - lets say it's like :-

the quick ==brown== fox jumps ==over== the lazy ==dog==

I want to replace the first == with ??, so start at index 0, then I want to know
that I have reached position whatever, and I want to replace the next == with
!!, and the next with ?? and so on alternately.

String has an "IndexOf" function, but using that in StringBuilder after each
replace seems very convoluted.

What would be the cleanest C# way to do this?

TIA

Jim
 
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.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

// If it is equal to the search string, then replace.
if (

}
 
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
 
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 said:
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
 
Jim said:
the quick ==brown== fox jumps ==over== the lazy ==dog==

I want to replace the first == with ??, so start at index 0, then I want to know
that I have reached position whatever, and I want to replace the next == with
!!, and the next with ?? and so on alternately.

Regex.Replace(Source, "(==(.*?)==)", "??$2!!");
 
Back
Top