Search Forum
(53671 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 Jargon in CSharp
By Prasad

The following examples illustrates the usage of C# String
class functions.It also list some additional functions which
are not currently available in C#.

using System;

class MyString
{
  public static void Main()
  {
    String strData="WeLcOmE tO c#.eNjoy FoLkS";

    Console.WriteLine("String Value: {0}",strData);
    Console.WriteLine("LowerCase Equivalent: {0}",Lower(strData));
    Console.WriteLine("UpperCase Equivalent: {0}",Upper(strData));
    Console.WriteLine("PCase Equivalent: {0}",PCase(strData));
    Console.WriteLine("Reverse Equivalent: {0}",Reverse(strData));
    Console.WriteLine("Is 'rotator' PalinDrome:{0}",IsPalindrome("rotator"));
    Console.WriteLine("Is 'visualc#' PalinDrome:{0}",IsPalindrome("visualc#"));
    Console.WriteLine("Left(string,5): {0}",Left(strData,5));
    Console.WriteLine("Right(String,6): {0}",Right(strData,6));
    Console.WriteLine("CharCount(Charcount,c):{0}",CharCount("Charcount","C"));
    Console.WriteLine("CharCount(CharCount,c,true):{0}",CharCount("Charcount","C",true));
    Console.WriteLine("CharCount(CharCount,d,true):{0}",CharCount("Charcount","d",true));
    Console.WriteLine("ToSingleSpace('welcome  to     C      Sharp'): {0}",ToSingleSpace("welcome  to     C      Sharp     "));
    Console.WriteLine("Replace(aaaaaa,aa,a):{0}",Replace("aaaaaa","aa","a"));
  }

  // Convert string to LowerCase
  public static String Lower(String strParam)
  {
    return strParam.ToLower();
  }

  //Convert String to UpperCase
  public static String Upper(String strParam)
  {
    return strParam.ToUpper();
  }

  //Convert String to ProperCase
  public static String PCase(String strParam)
  {
    String strProper=strParam.Substring(0,1).ToUpper();
    strParam=strParam.Substring(1).ToLower();
    String strPrev="";

    for(int iIndex=0;iIndex < strParam.Length;iIndex++)
    {
      if(iIndex > 1)
      {
        strPrev=strParam.Substring(iIndex-1,1);
      }
      if( strPrev.Equals(" ") ||
          strPrev.Equals("\t") ||
          strPrev.Equals("\n") ||
          strPrev.Equals("."))
      {
          strProper+=strParam.Substring(iIndex,1).ToUpper();
      }
      else
      {
        strProper+=strParam.Substring(iIndex,1);
      }
    }
    return strProper;
  }

  // Function to Reverse the String
  public static String Reverse(String strParam)
  {
    if(strParam.Length==1)
    {
      return strParam;
    }
    else
    {
      return Reverse(strParam.Substring(1)) + strParam.Substring(0,1);
    }
  }

  // Function to Test for Palindrome
  public static bool IsPalindrome(String strParam)
  {
    int iLength,iHalfLen;
    iLength=strParam.Length-1;
    iHalfLen=iLength/2;
    for(int iIndex=0;iIndex<=iHalfLen;iIndex++)
    {
      if(strParam.Substring(iIndex,1)!=strParam.Substring(iLength-iIndex,1))
      {
        return false;
      }
    }
    return true;
  }

  // Function to get string from beginning.

  public static String Left(String strParam,int iLen)
  {
    if(iLen>0)
      return strParam.Substring(0,iLen);
    else
      return strParam;
  }

  //Function to get string from end
  public static String Right(String strParam,int iLen)
  {
    if(iLen>0)
      return strParam.Substring(strParam.Length-iLen,iLen);
    else
      return strParam;
  }

  //Function to count no.of occurences of Substring in Main string
  public static int CharCount(String strSource,String strToCount)
  {
    int iCount=0;
    int iPos=strSource.IndexOf(strToCount);
    while(iPos!=-1)
    {
      iCount++;
      strSource=strSource.Substring(iPos+1);
      iPos=strSource.IndexOf(strToCount);
    }
    return iCount;
  }

//Not available in C#
  //Function to count no.of occurences of Substring in Main string
  public static int CharCount(String strSource,String strToCount,bool
IgnoreCase)
  {
    if(IgnoreCase)
    {
      return CharCount(strSource.ToLower(),strToCount.ToLower());
    }
    else
    {
      return CharCount(strSource,strToCount);
    }
  }

//Useful Function can be used whitespace stripping programs
  //Function Trim the string to contain Single between words
  public static String ToSingleSpace(String strParam)
  {
    int iPosition=strParam.IndexOf("  ");
    if(iPosition==-1)
    {
      return strParam;
    }
    else
    {
      return ToSingleSpace(strParam.Substring(0,iPosition) +
strParam.Substring(iPosition+1));
    }
  }

  //Function Replace string function.

// Currently Not Available in C#
  public static String Replace(String strText,String strFind,String
strReplace)
  {
    int iPos=strText.IndexOf(strFind);
    String strReturn="";
    while(iPos!=-1)
    {
      strReturn+=strText.Substring(0,iPos) + strReplace;
      strText=strText.Substring(iPos+strFind.Length);
      iPos=strText.IndexOf(strFind);
    }
    if(strText.Length>0)
      strReturn+=strText;
    return strReturn;
  }
}

Here is what the output should look like:

String Value: WeLcOmE tO c#.eNjoy FoLkS
LowerCase Equivalent:welcome to c#.enjoy folks
UpperCase Equivalent:WELCOME TO C#.ENJOY FOLKS
PCase Equivalent:Welcome To C#.Enjoy Folks
Reverse Equivalent:SkLoF yojNe.#c Ot EmOcLeW
Is 'rotator' Palindrome :True
Is 'visualc#' Palindrome:False
Left(string,5):WeLcO
Right(string,6):FolkS
CharCount(CharCount,c):1
CharCount(CharCount,c,true):2
CharCount(CharCount,d,true):0
ToSingleSpace('welcome to C Sharp '):Welcome to C Sharp
Replace(aaaaaa,aa,a):aaa