Build C# String Class And Methods

View: 378    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

File has been tested

C# defines a built-in string class which lives inside system namespace such as System.String

String class implements different types of interfaces

It is a sealed class that means we cannot inherit it with other classes

The purpose of string class is that it provides string-handling functionality

There are several built-in overloaded methods available inside a string class which helps us to manipulate and working with string objects in several different ways such as join one string to another, search a specific word in string, make a copy of strings etc

Along with methods the string class also defines a Length property which returns total characters contains a specific string.

Some String Class Methods:

Example – 1, Use of PadRight() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";
 
            // PadRight() method is called and displayed.
            Console.Write(siteName.PadRight(22, '*'));
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        Tutorialstown*********
     */
}

Example – 2, Use of StartsWith() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";
 
            if (siteName.StartsWith("tutorialstown",true, System.Globalization.CultureInfo.CurrentCulture))
                Console.WriteLine("Yes, it is a tutorialstown.com");
            else
                Console.WriteLine("No, it is not a tutorialstown.com");
 
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        Yes, it is a tutorialstown.com
     */
}

Example – 3, Use of trim() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "\t\t\tTutorialstown";
 
            Console.WriteLine("Before trim() method:");
            Console.WriteLine(siteName);
 
            Console.WriteLine("After trim() method:");
            Console.WriteLine(siteName.Trim());
 
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        Before trim() method:
                               Tutorialstown
        After trim() method:
        Tutorialstown
 
     */
}

Example – 4, Use of Length property:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";
 
            Console.WriteLine("Total characters are: {0}", siteName.Length);
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        Total characters are: 13
     */
}

Example – 5, Use of ToCharArray() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";
 
            // ToCharArray() method converts string into an character array.
            char[] charArray = siteName.ToCharArray();
 
            // Loop throug each element from char array and displayed.
            for (int index = 0; index < charArray.Length; index++)
 
                Console.WriteLine(index + " - " + charArray[index]);
 
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        0 - T
        1 - u
        2 - t
        3 - o
        4 - r
        5 - i
        6 - a
        7 - l
        8 - s
        9 - t
        10 - o
        11 - w
        12 - n
 
     */
}

Build C# String Class And Methods

There are several built-in overloaded methods available inside a string class which helps us to manipulate and working with string objects in several different ways such as join one string to another, search a specific word in string, make a copy of strings etc

Posted on 06-09-2016 

Comment:

To comment you must be logged in members.

Files with category

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd