String class in C#

CsharpProgrammingServer Side Programming

The string keyword is an alias for the System.String class. The String class has the following two properties −

Sr.No.Property & Description
1Chars
Gets the Char object at a specified position in the current Stringobject.
2Length
Gets the number of characters in the current String object.

The following are some of the methods of String class −

Sr.No.Methods & Description
1public static int Compare(string strA, string strB)
Compares two specified string objects and returns an integer that indicates their relative position in the sort order.
2public static int Compare(string strA, string strB, bool ignoreCase )
Compares two specified string objects and returns an integer that indicates their relative position in the sort order. However, it ignores case if the Boolean parameter is true.
3public static string Concat(string str0, string str1)
Concatenates two string objects.
4public static string Concat(string str0, string str1, string str2)
Concatenates three string objects.
5public static string Concat(string str0, string str1, string str2, string str3)
Concatenates four string objects.

Let us see how to create a string and join a string in the following example −

Example

using System;

namespace StringApplication {

   class StringProg {

      static void Main(string[] args) {
         string[] starray = new string[]{"Cricket is my life",
         "It is played between two teams",
         "It has three formats",
         "T20, Test Cricket and ODI",
         "Cricket is life"
         };

         string str = String.Join("\n", starray);
         Console.WriteLine(str);
      }
   }
}
raja
Published on 14-Aug-2018 09:16:12
Advertisements