.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

I can't get my head around case-sensitive string comparison

Hi,
I can’t get my head around this. I’m comparing 2 strings, I ‘m looking for a complete match (case sensitive if chkBoxMatchCase.Checked) or if there is a matching substring (also case sensitive if chkBoxMatchCase.Checked). Can anyone advise how I should rewrite the few line below to achieve this? The problem with ((Value2.ToUpper() == Value1.ToUpper()) in the code below is that I’m not really looking within the string which I need to do as well as comparing the full string.
Major thanks, Steve

Value1 = A quick brown fox
Value2 = Fox
if (((Value1.Contains(Value2)) && (!chkBoxValue1IsBlank.Checked)) || ((Value2.ToUpper() == Value1.ToUpper()) && (!chkBoxMatchCase.Checked) &&  (!chkBoxValue1IsBlank.Checked)) || ((Value1 == "") && (Value1IsBlank.Checked)))

{
Dothis();                
}


Many thanks for the help.
Steve
Friday, January 25, 2008
 
 
bool CompareString(string s1, string s2, bool IgnoreCase)
{
  if (string.Compare(s1, s2, IgnoreCase)) return true;

  if (IgnoreCase)
  {
    s1 = s1.ToUpper();
    s2 = s2.ToUpper();
  }

  if (s1.Contains(s2)) return true;
  if (s2.Contains(s1)) return true;
}
Joel Coehoorn Send private email
Friday, January 25, 2008
 
 
Really the first line of that is not necessary, since the contains would still work if the strings match.  I used it just to point out the function to you and because the framework probably does some work behind the scenes so it doesn't have to create two completely new strings to do the case insensitive compare.
Joel Coehoorn Send private email
Friday, January 25, 2008
 
 
It's also impossible for the shorter one to contain the longer one, so if performance is critical you could take a shortcut there.

Friday, January 25, 2008
 
 
something like this:

string value1 = "A quick brown fox";
string value2 = "Fox";

if (chkValue1IsBlank.Checked ||
    (!chkMatchCase.Checked && value1.IndexOf(value2) >= 0) ||
    (chkMatchCase.Checked && value1.ToUpper().IndexOf(value2.ToUpper()) >= 0))
{
    DoThis();               
}

Couple of micro-design notes:

1. Ideally, there shouldn't be a situation when both the checks for chkValue1IsBlank.Checked and value1 == "" are required. UI can disable value1 entry field when chkValue1IsBlank is checked, while back-end should probably treat violation of this as any other data inconsistency.

2. Short-circuit evaluation are useful. In the _if_ expression, the original code did something, and then checked if it should be doing it in the first place.

3. Now you can call me a pedant :P
DK
Friday, January 25, 2008
 
 
A Note on coding style.

Value1 = A quick brown fox
Value2 = Fox
if (((Value1.Contains(Value2)) && (!chkBoxValue1IsBlank.Checked)) || ((Value2.ToUpper() == Value1.ToUpper()) && (!chkBoxMatchCase.Checked) &&  (!chkBoxValue1IsBlank.Checked)) || ((Value1 == "") && (Value1IsBlank.Checked)))

{
Dothis();               
}

vs.
 
bool CompareString(string s1, string s2, bool IgnoreCase)
{
  if (string.Compare(s1, s2, IgnoreCase)) return true;

  if (IgnoreCase)
  {
    s1 = s1.ToUpper();
    s2 = s2.ToUpper();
  }

  if (s1.Contains(s2)) return true;
  if (s2.Contains(s1)) return true;
}

Why can't people write code like Joel does?  Do people really think craming everything into a line make them more professional?

Saturday, January 26, 2008
 
 
Creating additional string objects by calling ToUpper is less efficient than comparing the existing strings.
If you are using .NET 2.0 or later, you could do something like:

bool CompareString(string s1, string s2, bool ignoreCase)
{
  // Handle null arguments in whatever way you want
  // E.g. if you don't allow null arguments throw an
  // ArgumentNullException.
  // Alternatively you could return true if both are null
  // and false if exactly one is null.
  if (s1 == null) throw new ArgumentNullException("s1");
  if (s2 == null) throw new ArgumentNullException("s2");

  // Get the comparison rule.  The following assumes you
  // want to use the current culture.
  StringComparison comparisonType = ignoreCase ? StringComparison.CurrentCultureIgnoreCase :
StringComparison.CurrentCulture;

  // Return value is true if either string contains the
  // the other.
  if (s1.IndexOf(s2, comparisonType) >= 0) return true;
  if (s2.IndexOf(s1, comparisonType) >= 0) return true;

  return false;
}

If you are using .NET 1.x you can do something similar using CultureInfo.CurrentCulture.CompareInfo.IndexOf.
Joe
Wednesday, January 30, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz