Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

String extractors and manipulators in C# - Part IV
By J.V.Ravichandran

The "toupper" and "tolower" methods, in the following class, are both similar except for the one line conversion of the 'int' type data member ucode, which represents the ASCII value of the character read from the string or keyboard, to lowercase or uppercase. After determining the length of the string passed to it, both the methods scan each character in the string into a 'char' type data member and then convert the character to the opposite case after evaluating the case in an if...else block. Quite simple ! And this simplicity is because of the indexer of the class provided by C#, which allows for each character in the string to be scanned as if from an array of strings ! Both methods return objects and receive objects hence are defined of type object.

The following is the class which converts characters in a string into uppercase or lowercase

using system;

class convertstr
{
 private string s1; // The string type data member which is used to return the
                    // converted string
 private int ucode; // The int type data member which is used to 
 public string toupper(string s2) // The toupper method returning a string object
 {
 public int strlen=s2.Length; // Determines the string length for the 'for' loop
 for (int i=0;i < strlen;i++) // Reads each character in string via the indexer
 {
  char ch=s2[i];
 if (ch > ='a' && ch < ='z')  // Determines if the character is an alphabet and in {    
                                  // lower case
 ucode=(int)ch;
 ucode=ucode-32; 
 s1[i]=(char)ucode;
 }
 else    // else retains the same case into the string object of       {    
         // the class
 s1[i]=ch;
 }
 }
 return s1;   // Method returns converted string
 }
 public string tolower(string s4) // The tolower method returning a string object
 {
 public int strlen=s4.Length;
 for (int i=0;i < strlen;i++) // Reads each character in string via the indexer
 {
 char ch=s4[i];
 if (ch > ='A' && ch < ='Z')  // Determines if the character is an alphabet and in 
 {    // upper case
 ucode=(int)ch;
 ucode=ucode+32;
 s1[i]=(char)ucode;
 }
 else    // else retains the same case into the string object of     
 {    // the class
 s1[i]=ch;
 }
 return s1;   // Method returns converted string
 }
}