• Now Online : 77
  • admin@codemyne.net

Introduction

Strings are a way to combine a group of characters for presentation to the user. C# has the built in support for string class. Another advantage of the .NET string class is that it produce nonmutable strings. This prevents a string from being overwritten because a pointer was being used to access the string, as sometimes occurred with C++ strings. If you truely desire to have a mutable string, you should use the StringBuilder class from the System.Text namespace.

C-style strings are nothing more than an array of chars that is null terminted. To define a C-style string, create a char array like, char[] mystring = new char[6] {'h','e','l','l','o'}.

Another more preferred way to handle strings is to use the built-in string class, which provides a variety of methods that perform common tasks. To define and declare a new string class, use the string keyword. string MyString = new string("hello");
This creates a new string that will automatically null terminated (\0).

Note: By default, strings are unicode in C#. It is also important to remember that the C# string keyword is an alias for System.String.

using System;
        namespace stringintroduction
        {
        class stringcreation
        {
            static void main(string[] args)
            {
                char MyChar='A';
                MyChar = (char)65;
                //creates a unicode array
                char[] MyChar2={'h','e','l','l','o','\0'};
                char[] MyChar3=new char[5];
                MyChar[0]='h';
                MyChar[1]='e';
                MyChar[2]='l';
                MyChar[3]='l';
                MyChar[4]='o';
                MyChar[5]='\0';
            }
        }
    }
    using System;
    namespace MyNamespace
    {
        class MyMainClass
        {
            static void main(string[] args)
            {
                //create a string
                string MyString="hello";
            }
        }
    }

String Concatenation: String concatenation is used to add characters to the end of a string. The string class provides two methods for adding characters to a string:
1. The INSERT method allows you to specify where to place the new characters in the string. It has two parameters: the location where you would like to add to the string, followed by the string to be added to the existing string.
2. The APPEND method allows you to add a string to the end of an existing string. The only parameter you pass to this method is the string to be added.

using System;
using System.Text;
    namespace stringintroduction
    {
        class Stringconcatenation1
        {
            static void Main(string[] args)
            {
                //create mutable string
                StringBuilder MyString=new;
                StringBuilder("hello");
                MyString.Insert(0, "My");
                MyString.Append("world");
                Console.WriteLine(MyString);
            }
        }
    }

The StringBuilder class is convenient for string concatenation, as well as other types of string modifications, such as removing, replacing, or inserting characters. Unlike the string class, which creates an entire new object for each change to the string, with the StringBuilder class, you can avoid creating a new string subsequent to each modification. The methods contained within this class do not return a new StringBuilder object, unless you specify otherwise. This makes the StringBuilder class a much better solution for operating on strings.


The following examples demonstrate string concatenation with mutable and nonmutable strings.
using System;
    namespace stringintroduction
    {
        class Stringconctnation2
        {
            static void main(string[] args)
            {
                Console.WriteLine("Enter your password:");
                string userpassword=Console.ReadLine();
                //create a nonmutable string
                string password ="victory";
                //compare the value of the string
                if(Password.CompareTo(UserPassword)==0)
                {
                    Console.WriteLine("Bad Password");
                }
                Console.WriteLine("Good Password");
            }
        }
    }

Substrings: String within a string is called substring, these will work in several ways. These include searching for substrings and extracting substrings.
Searching for Substrings There are many ways to find a string within a string. The string class provides two methods that will determine if a string contains a particular substring.
1. The StartsWith method returns true if the string passed as the first parameter is contained at the beginning of the string being searched.
2. The EndsWith method returns true if the string passed as the first parameter is found at the end of an existing string.
The following example demonstrates searching for substring.

using System;
 namespace stringintroduction
    {
        class substrings
        {
            static void main(string[] args)
            {
                //create an array of strings
                string FootballTeams=new string[3] {"miami, dolphins", "oakland,raiders","seattle, seahawks"};
                //iterates through each string in the array
                foreach(string s in FootballTeams)
                {
                    //returns awesome if the string starts with miami
                    if(s.StartsWith("miami"))
                    Console.WriteLine("awesome!");
                    else
                    Console.WriteLine("Bummer Dude");
                }  
            } 
        }
    }

Extracting Substrings: The substring method of the string class allows to provide a parameter that will retrieve the specified number of characters from the beginning of an existing string. There are many more overloaded forms of this method that provide even more functionality.
The following example demonstrates extracting substrings.

using System;
    namespace stringintroduction
    {
        class extractingsubstrings
        {
            static void main(string[] args) 
            {
                string Myclasses="math 101-algebra";
                //create a string that is made up of the first six chars of Myclasses
                string Mysubstring=Myclasses.Substring(6);
                Console.WriteLine(Mysubstring);
            }
        }
    }

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment