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 5 8980
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]
- mv*@spam.guard.caspershouse.com
// If it is equal to the search string, then replace.
if (
}
"Jim Lawton" <uc**@use.your.initiative> wrote in message
news:g1********************************@4ax.com... 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
On Tue, 4 Jan 2005 09:21:04 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@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
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" <uc**@use.your.initiative> wrote in message
news:7g********************************@4ax.com... On Tue, 4 Jan 2005 09:21:04 -0500, "Nicholas Paldino [.NET/C# MVP]" <mv*@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
Jim Lawton wrote: 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!!");
--
programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
On Tue, 04 Jan 2005 10:58:38 -0800, Jon Shemitz <jo*@midnightbeach.com> wrote: Jim Lawton wrote:
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!!");
Thanks Jon, that's just what I was looking for!
Jim This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: spam |
last post by:
Is there a well-known algorithm for replacing many substrings in a
string? For example, I'd like to take the string "abc def ghi jkl mno
pqr" and replace, say, every instance of "abc", "ghi", and...
|
by: Will McGugan |
last post by:
Hi,
Is there a simple way of replacing a large number of substrings in a
string? I was hoping that str.replace could take a dictionary and use it
to replace the occurrences of the keys with the...
|
by: C3 |
last post by:
I have to process some data in C that is given to me as a char * array. I
have a fairly large number of substrings (well, they're not actually
printable, but let's treat them as strings) that I...
|
by: Brett |
last post by:
I've been trying different methods for replacing escaped strings. None
work. I even have a custom Replace() method but still can't get what I
need. Some of the characters I need to replace are:...
|
by: Jon Smirl |
last post by:
I only have a passing acquaintance with Python and I need to modify some
existing code. This code is going to get called with 10GB of data so it
needs to be fairly fast.
...
|
by: amadain |
last post by:
Hi
I was wondering if there was a nicer way to swap the first 2
characters in a string with the 4th and 5th characters other than:
darr=list("010203040506")
aarr=darr
barr=darr
darr=barr...
|
by: DarthBob88 |
last post by:
I have to go through a file and replace any occurrences of a given
string with the desired string, like replacing "bug" with "feature".
This is made more complicated by the fact that I have to do...
|
by: Choi |
last post by:
Hi !
I wonder how I can replace a quote by a "\n" in a string.
For example my string is rrrrrrr'ttttttt and I want to obtain :
rrrrrrr
ttttttt
I tried the function find, but I don't know...
|
by: Polarism |
last post by:
Hello,
I am very new to perl and I am having trouble figuring out how to replace multiple strings in a single file. The file has something around 750k instances that need to be replaced with 350...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |