C# String Methods


The String class has many methods that help you in working with the string objects. The following table has some of the most commonly used methods −

Sr.NoMethod & 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.
6public bool Contains(string value)
Returns a value indicating whether the specified String object occurs within this string.
7public static string Copy(string str)
Creates a new String object with the same value as the specified string.
8public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
Copies a specified number of characters from a specified position of the String object to a specified position in an array of Unicode characters.
9public bool EndsWith(string value)
Determines whether the end of the string object matches the specified string.
10public bool Equals(string value)
Determines whether the current String object and the specified String object have the same value.
11public static bool Equals(string a, string b)
Determines whether two specified String objects have the same value.

Let us see an example to work with Contains() method in C#. The Contains(string value) returns a value indicating whether the specified String object occurs within this string.

Example

using System;
namespace Demo {

   class Program {

      static void Main(string[] args) {
         string str = "This is test";
     
         if (str.Contains("test")) {
            Console.WriteLine("Yes, 'test' was found.");
         }
         Console.ReadKey() ;
      }
   }
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

498 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements