4/01/2010

C# String Functions



A string is sequence of characters enclosed within a pair of double quotes.
example: "http://dotnet-skills.blogspot.com","Mohammad","123@$#%"

Some of the built in string handling functions are stated below:
1. Length
2. ToUpper()
3. ToLower()
4. IndexOf()
5. LastIndexOf()
6. Split()
7. Replace()
8. Remove()
9. StarstWith()
10.EndsWith() 
11.Equals()
12.Contains()
13.Substring()
14.ToCharArray()
15.CompareTo()
16.Trim()
17.TrimStart()
18.TrimEnd()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Strings
{
    class Program
    {
        static void Main(string[] args)
        {
            /* StartsWith(): returns true if the substring is present at 
            the beginning of the string. otherwise, it returns false.*/

            string s1 = "http://dotnet-skills.blogspot.com";
            Console.WriteLine("Result: " + s1.StartsWith("http"));

            //EndsWith(): returns true if the substring is present at 
            // the end of the string. otherwise, it returns false.

            Console.WriteLine("Result: " + s1.EndsWith(".com"));

            //Equals(): used to compare two strings

            string s2 = "dotnet-skills";
            string s3 = "dotnet-skills";

            if (s2.Equals(s3))
                Console.WriteLine("strings are equal");
            else
                Console.WriteLine("strings are not equal");

            //Contains( ): returns true if the substring is present the given
            // string. otherwise, return false.

            string s4 = "http://dotnet-skills.blogspot.com";
            Console.WriteLine(s4.Contains("dotnet"));

            //Substring(): extracts the substring from the given string

            string s5 = "http://dotnet-skills.blogspot.com";
            Console.WriteLine("Substring: " + s5.Substring(7, 13));

            // ToCharArray(): creates array of characters for a given string

            string s6 = ".net technology";

            char[] a = s6.ToCharArray();

            for (int i = 0; i < a.Length; i++)
                Console.WriteLine(a[i]);

        }
    }
}

Out put:
Result: True
Result: True
strings are equal
True
Substring: dotnet-skills
.
n
e
t

t
e
c
h
n
o
l
o
g
y

No comments:

Post a Comment