Monday, 25 July 2016

C sharp String

A string in CSharp is an object of type String whose value is text. The string type represents a string of Unicode Characters. String objects are immutable that is they cannot be changed after they have been created. String is an alias for System.String in the.NET Framework. Initialize a string with the Empty constant value to create a new String object whose string is of zero length.

C# string Clone - Clone() method creates and returns a copy of string object. The CSharp string Clone() method returns a reference to this instance of string.
Example
using System;
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Test string";
            string newstr = null;
            newstr = (String)str.Clone();
            Console.WriteLine(newstr);
            Console.ReadLine();
        }
    }




 C# string Compare- the CSharp String Compare function compares two strings. The comparison is based on the Unicode value of each character in the string. It returns an Integer indication lexical relationship between the two comprehends.
Example
using System;
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = null;
            string str2 = null;

            str1 = "csharp";
            str2 = "CSharp";

            int result = 0;

            result = string.Compare(str1, str2);
            Console.WriteLine(result);

            result = string.Compare(str1, str2, true);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }

Note
Integer : returns less than zero, zero or greater than zero.
Less than zero : str1 is less than str2
zero : str1 is equal to str2
Greater than zero : str1 is greater than str2


 C# string Concat
Example
using System;
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = null;
            string str2 = null;
            string newstr = null;

            str1 = "csharp";
            str2 = "examples";

            newstr = String.Concat(str1, str2);         
            Console.WriteLine(newstr);
            Console.ReadLine();
        }
    }

 C# string Contains
Example
  using System;
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = null;
            str1 = "csharp best examples";
            Console.WriteLine(str1.Contains("best"));
            Console.ReadLine();
        }
    }


 C# string Copy
Example
using System;
    class Program
    {
        static void Main(string[] args)
        {
            string str1 ="csharp best examples";
            string str2 = null;
            str2 = string.Copy(str1);
            Console.WriteLine(str2);
            Console.ReadLine();
        }
    }



 C# string CopyTo - CSharp string CopyTo method Copies a specified number of characters from a specified position in this instance to a specified position in an array of characters.
using System;
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "welcome to examples guru";
            char[] chrs = new char[6];
            str1.CopyTo(0, chrs, 0, 6);
            foreach (var item in chrs)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }


C# string EndsWith
Example
using System;
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "welcome to examples guru";
            Console.WriteLine(str1.EndsWith("guru"));
            Console.ReadLine();
        }
    }


 C# string Equals
 C# string Format
 C# string IndexOf
 C# string Insert
 C# string Length
 C# string Split
 C# string Substring
 validate a string using TryParse in C#

 C# String Null


No comments:

Post a Comment