C#
  Home arrow C# arrow Page 3 - Strings and Characters, Part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Strings and Characters, Part 1
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 17
    2004-07-21

    Table of Contents:
  • Strings and Characters, Part 1
  • 2.2 Determine if a character is in a Specified Range
  • 2.3 Controlling Case Sensitivity when Comparing Two Characters
  • 2.4 Finding All Occurrences of a Character Within a String
  • 2.5 Finding the Location of All Occurrences of a String Within Another String
  • 2.6 The Poor Man’s Tokenizer Problem
  • 2.7 Controlling Case Sensitivity when Comparing Two Strings
  • 2.8 Comparing a String to the Beginning or End of a Second String
  • 2.9 Inserting Text into a String
  • 2.10 Removing or Replacing Characters Within a String
  • 2.11 Encoding Binary Data as Base64
  • 2.12 Decoding a Base64-Encoded Binary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Strings and Characters, Part 1 - 2.3 Controlling Case Sensitivity when Comparing Two Characters


    (Page 3 of 12 )

    Problem

    You need to compare two characters for equality, but you need the flexibility of performing a case-sensitive or case-insensitive comparison.

    Solution

    Use the Equals instance method on the char structure to compare the two characters:

    public static bool IsCharEqual(char firstChar, char secondChar)
    {

    return (IsCharEqual(firstChar, secondChar, false));
    }

    public static bool IsCharEqual(char firstChar, char secondChar,

    bool caseSensitiveCompare) {

    if (caseSensitiveCompare)

    {

    return (firstChar.Equals(secondChar));

    }

    else

    {

    return (char.ToUpper(firstChar).Equals(char.ToUpper(secondChar)));
    }
    }

    The first overloaded IsCharEqual method takes only two parameters: the characters to be compared. This method then calls the second IsCharEqual method with three parameters. The third parameter on this method call defaults to false so that when this method is called, you do not have to pass in a value for the caseSensitiveCompare parameter—it will automatically default to false.

    Discussion

    Using the ToUpper method in conjunction with the Equals method on the string class allows us to choose whether to take into account the case of the strings when comparing them. To perform a case-sensitive comparison of two char variables, simply use the Equals method, which, by default, performs a case-sensitive comparison. Performing a case-insensitive comparison requires that both characters be converted to their uppercase values (they could just as easily be converted to their lowercase equivalents, but for this recipe we convert them to uppercase) before the Equals method is invoked. Setting both characters to their uppercase equivalents removes any case-sensitivity between the character values, and they can be compared using the case-sensitive Equals comparison method as though it were a case-insensitive comparison.

    You can further extend the overloaded IsCharEqual methods to handle the culture of the characters passed in to it:

    public static bool IsCharEqual(char firstChar, CultureInfo firstCharCulture, char secondChar, CultureInfo secondCharCulture)
    {
    return (IsCharEqual(firstChar, firstCharCulture, secondChar, secondCharCulture, false));
    }

    public static bool IsCharEqual(char firstChar, CultureInfo firstCharCulture, char secondChar, CultureInfo secondCharCulture, bool caseSensitiveCompare)

    {
    if (caseSensitiveCompare)
    {

    return (firstChar.Equals(secondChar));
    }
    else
    {

    return (char.ToUpper(firstChar, firstCharCulture).Equals (char.ToUpper(secondChar, secondCharCulture)));
    }
    }

    The addition of the CultureInfo parameters to these methods allows us to pass in the culture information for the strings that we are calling ToUpper on. This information allows the ToUpper method to correctly uppercase the character based in the culture-specific details of the character (i.e., the language, region, etc., of the character).

    Note that you must include the following using directives to compile this code:

    using System;
    using System.Globalization;

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More C# Articles
    More By O'Reilly Media


     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT