On Thu, Dec 11, 2008 at 11:58 PM, Praveen Kumar <[email protected]> wrote:
> Hi,
>
> You can write program for Comparing string in C# without using
> String.Compare() function.
>
> bool CompareString(string source, string dest)
>
> {
>
> if(source.Length != dest.Length)
>
> return false;
>
> int sourceLen = source.Length;
>
> for(int iCounter = 0; iCounter < sourceLen; iCounter++)
>
> {
>
> if(source.Substring( iCounter, 1) != dest.Substring( iCounter, 1))

Akk!  Huh?  Wud?  Substring() is no way to compare strings.  Strings
are immutable, so your code is creating new strings for every comparison.

Reading what the parent posted it just says to not use the Compare() function.

Personally, I would call the "teacher" on his/her own words.  Just use
the Equals()
method with something like ToUpper().

So:

If (s.ToUpper.Equals(p.ToUpper())
{
  MessageBox.Show("Teacher, create better lesson plans!.");
}

Since MSDN states:

"Although string is a reference type, the equality operators (== and !=) are
defined to compare the values of string objects, not references".

Ah.  So you don't need the Compare() method at all.

You can also just use each string as an array and check each char of the array.

Just a hint:
if (s[0] == p[0]) // don't do it this way.  Do a loop.

Jim

Reply via email to