Re: [Mono-list] Problems with CopyTo, using Contains(...) and inserting a character from one string into another.

2009-02-23 Thread Chris Howie
On Tue, Jan 27, 2009 at 6:53 AM, PFJ pjohns...@uclan.ac.uk wrote:
 I'm trying to copy a specific section of a string to another string.
 My code looks like this

 void search(string s)
 string newstring;
 for (int a = 0 ; a  s.Length; ++a)
 {
  if (a + 1  s.Length)
   continue;
  if (s[a + 1]  'a')
   s.CopyTo(a, newstring.ToCharArray(), 0, 1);
  else
  {
   s.CopyTo(a, newstring.ToCharArray(),0, 2);
   a++;
  }
 }

I did not look too deeply into the exception you are getting because
this approach is simply wrong.  You are copying the characters into a
new array that you never use again.  In other words, this block of
code would do absolutely nothing but burn CPU cycles even if it did
work.

You probably want to look at the StringBuilder class as well.  It
better encapsulates actions like this and is designed to prevent you
from creating several intermediate String objects (which you don't do
here but are very likely to do if you haven't done this kind of thing
before).

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


[Mono-list] Problems with CopyTo, using Contains(...) and inserting a character from one string into another.

2009-02-22 Thread PFJ

Hi,

I'm trying to copy a specific section of a string to another string.
My code looks like this

void search(string s)
string newstring;
for (int a = 0 ; a  s.Length; ++a)
{
 if (a + 1  s.Length)
   continue;
 if (s[a + 1]  'a')
   s.CopyTo(a, newstring.ToCharArray(), 0, 1);
 else
 {
   s.CopyTo(a, newstring.ToCharArray(),0, 2);
   a++;
 }
}

However, I'm getting an exception out of range error - even if a = 0.
If I replace newstring.ToCharArray with a true char array, I get the
same error.

Have I misunderstood copyto? It looks ok according to MSDN.

Another niggle, is there anyway to make something akin to Contains but
compares not just the first character, but for all given characters
(for example, my string contains BaSO4, if I use Contains, then B gets
seen first from the comparison list, it exits, which means that as e
doesn't exist and the code fails)? I've tried

if (System.Text.RegularExpressions.Regex.IsMatch(dupeform, element,
System.Text.RegularExpressions.RegexOptions.IgnoreCase))
and not to ignore the case, but that fails too.

Help!

TTFN

Paul
-- 
View this message in context: 
http://www.nabble.com/Problems-with-CopyTo%2C-using-Contains%28...%29-and-inserting-a-character-from-one-string-into-another.-tp21683670p21683670.html
Sent from the Mono - General mailing list archive at Nabble.com.

___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list