CopyPastehas never been so tasty!

Use of StartsWith, EndsWith and Copy string in C#

by anonymous

  • 0
  • 0
  • 0
65 views

Orignal Source:http://codingvilla.com/startswith-endswith-copystring-csharp-single-article-608.aspx

 

StartsWith Function:

 

StartsWith method is used to check the starting characters of string. You can check the starting characters of a string against another string whether they are equal or not? You can also compare the string against the collection of strings by using foreach loop.­

 

The syntax of starts with is

 

string.StartsWith(“I am a boy”)

 

It takes only 1 parameter and that is only a string.

 

To understand let us consider this example in this we compare a string with another string using StartsWith method if it matches this will return true.

 

To demonstrate just make an console application and write the following code

 

Code:

 

classStartsWith_method

    {

staticvoid Main(string[] args)

        {

try

            {

 

string input = "i am a good friend";

 

 

if (input.StartsWith("i") ||

input.StartsWith("i am"))

                {

 

Console.WriteLine(true);

                }

Console.ReadLine();

            }

catch (Exceptionexce)

            {

Console.WriteLine(exce);

            }

        }

    }

 

 

EndsWith Function:

 

Ends with method is used to check the last characters of the string. You can compare starting characters of a string against another string to check whether they are equal or not? You can also compare the string against the collection of strings by using foreach loop.­

 

It is very easy way to test for ending substrings. The syntax of Endswith is

 

String.EndsWith(“I am boy”);

 

It takes only 1 string as parameter.

 

To understand the working of endswith method let us consider an example. First of all we declare a string and an array of strings. And then iterate that array in foreach loop and

 

check the last part of string by using endswith method.

 

To demonstrate make a console application and write the following code.

 

 

Code:

 

 

classEndsWith_method

    {

 

staticvoid Main(string[] args)

        {

try

           {

string s1 = "he is a boy";

string[] s2 = { "boy","girl","child"};

foreach (strings in s2)

            {

if (s1.EndsWith(s))

                {

Console.WriteLine(s);

Console.ReadLine();

                }

            }

        }

catch (Exceptionexce)

           {

Console.WriteLine(exce);

           }

    }

 

    }

 

 

 

Copy string:

 

In c# there are very exceptional cases in which we use string.Copy method therefore string.Copy method is not commonly used. It just Copies the value of string.

 

Syntax of CopyString is

 

String.Copy(s1)

 

It takes a string as parameter.

 

To understand let us consider an example, this example just copies a value from one string to another. It just Copies the value and their references couldn’t be equal because it just copy the value of string not references.

 

To demonstrate make a console application and write the following code.

 

 

Code:

 

 

classCopyString

    {

staticvoid Main(string[] args)

        {

try

            {

string s1 = "He is a Boy";

string s2 = string.Copy(s1);

Console.WriteLine(s1);

Console.WriteLine(s2);

Console.ReadLine();

 

 

            }

catch (Exceptionexce)

            {

Console.WriteLine(exce);

            }

        }

    }

 

 

Output:

 

He is a Boy

 

 

He is a Boy

 

Add A Comment: