Thanks Jon and Mark for your responses.
Clarifying two things - yes, the strings are of same length. And all I
have to worry about is location(s) of 'A' in the first string and have
another string (say str3) that is same as str2 but has 'A' s in it at
the same locations as str1. I could also overwrite str2 and slot in the
'A's based on str1.
I can see Mark's solution - clean and simple. But really I would like
to avoid looping through each and every character - Imagine if my
string size is 1000 and I am doing this operation thousands of times!
I am also looking at bit-wise operations and see whether it is possible
to do a bit operation to get what I want.
The problem is still open so I would appreciate comments.
Thanks.
RP
Hi Rohit,
as well as using regular expressions another possibility is using a
StringBuilder:
//PREDICATE: str1,str2 are always the same length
string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";
StringBuilder s1 = new StringBuilder(str1);
StringBuilder s2 = new StringBuilder(str2);
for (int i = 0; i < s1.Length; ++i)
{
if (s1
== 'A')
{
s2 = 'A';
}
}
Which is very clear (in my opinion clearer than using a regular
expression)
and efficient.
Mark.
--
http://www.markdawson.org
I am looking for a way to copy a pattern (letter 'A' in the following
example) to another string.
string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";
After the copy str2 becomes "1111AAAA000011AA".
Can this is done efficiently using Regex?
TIA
Rohit