String Methods Implementation
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
String Methods Implementation
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Building a simple Notepad Windows Application [next Lesson]  Subtle Programming Techniques
String Methods Implementation
  by: mosessaur

String Methods Implementation

by: mosessaur

Introduction:

This Tutorial will show us how to build a new class that use some String Methods to implement our own strings Methods Like: Convert String to ProperCase, Replace substring with another string, Insert a string in a specific position in another string, Reverse the string etc....

To implement those methods is very easy, but also we will see how to build them in .DLL to be library that we can add it as a reference to our project.

Building CString Class Library:

Now we will need to start new project in VS.Net, but we will chose Class Library Project, then we will build our class simply I'll call CString.

Methods

We have about 12 method to be implemented, I am going to talk about the most important Methods, and the other you can check for their code in the attached project. All methods is static methods, and at the end I'll show you how you can implement them so that you can instantiate an object from them and use those methods through that object.

First Mtehod we are going to talk about is the ProperCase Method

The Main function of this method is to return the string to its proper-case means if you have a string like this:

We ARe CSharpFriends MeMberS?!!

the result will be:

We Are Csharpfriends Members?!!

public static string ProperCase(string strValue)
{
    // Convert the 1st Char in the String to UpperCase and
    // store it in our return string.
    string strProper = strValue.Substring(0,1).ToUpper();

    // Convert the Rest of the String to LowerCase and
    // over write the original string
    strValue = strValue.Substring(1).ToLower();
    
    string strPrev = "";

    for(int intIndex=0; intIndex{
        if(intIndex>1)
        {
            //put the Previous Char in  strPrev
            strPrev = strValue.Substring(intIndex-1, 1);
        }
        //Search for White Space or Tab or new line or Dot in the strPrev,
        // that is mean new word starts with UpperCase
        if( strPrev.Equals(" ") || strPrev.Equals("\t") ||
                      strPrev.Equals("\n") || strPrev.Equals(".") )
        {
            //new word means 1st char convert to UpperCase
            strProper += strValue.Substring(intIndex, 1).ToUpper();
        }
        else
        {
            //No New word so take on Char and add it to the strProper.
            strProper += strValue.Substring(intIndex, 1);
        }
    }
    return strProper;
}
2nd Method is The Reverse Method, this method reverse the string like this:

Cairo ------> oriaC

This Method is Recursion Method
public static string Reverse(string strValue)
{
    // this is the Termination of the Recursion when the string
        // length is 1 means on char
    if(strValue.Length==1)
    {
        return strValue;
    }
    else
    {
        //print the or store the 1st char every time,
        //so that when 1st Char stored will be the end of the string
        //this way the string will be reversed
        return Reverse( strValue.Substring(1) ) + strValue.Substring(0,1);
    }
}
3rd Method is CharCount Method, This Method count number of occurences of Substring in main string:
public static int CharCount(string strSource,string strCountString)
{
    int intCount = 0;

    //Find the Position of the 1st Match
    int intPos = strSource.IndexOf(strCountString);
    
    while(intPos != -1)
    {
        //Increase Count
        intCount++;

        //Update the Original String
        strSource = strSource.Substring(intPos+1);

        //Find the next Match
        intPos = strSource.IndexOf(strCountString);
    }
    return intCount;
}
4th Method IsPalindrome check of the String is Palindrome string or not.

moAom (Palindrome)

Moon (not Palindrome)
public static bool IsPalindrome(string strValue)
{
    int intLen, intStrPartLen;
    intLen = strValue.Length - 1;

    //Cut the length of the string into 2 halfs
    intStrPartLen = intLen / 2;
    
    for(int intIndex = 0; intIndex <= intStrPartLen; intIndex++)
    {
        //intIndex is the index of the char in the front of the string
        //Check from behind and front for match
        if(strValue[intIndex] != strValue[intLen])
        {
            return false;
        }
        //decrease the lenght of the original string to
                //test the next Char from behind
        intLen--;
    }
    return true;
}
To check the rest of the function you can check the source code of the project.

Include the DLL in our application:

If you finished from compiling your CString Class as .dll, then start new windows application project, From the Solution Explorer window right click on the References node then click on add Reference, then Click on the Browse button on the dialog to browse your .dll file, when finished click Ok.

now in your code editor write in the using section this:

//this my namespace I made
using CStringLib; 
so you can write your owen name space you made in the using section, after that you are free to use your static methods from CString class.

Create non-Static Methods:

If you want to do so, so that you can use the methods through your instance you can do this as an Example:

public class CString
{
    string myString;
    public CString(string str)
    {
        myString = str; 
    }
    
    //this method will cut the string from left to the specified Length
    public  string Left(int intLen)
    {
        if(intLen > 0)
        {
            string strTemp = myString.Substring(0, intLen);
            return strTemp
        }
        else
        {
            return myString;
        }
    }
} 
now how you can instantiate an object from this class??

// this way you create an object of your class
CString myCString = "Hello World"; 

//this way you can call your methods
myCString = myCString.Left(4); 
Download the complete source code

String Class Library

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
String Methods Implementation
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Building a simple Notepad Windows Application [next Lesson]  Subtle Programming Techniques


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
eluvan 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center