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.No | Method & Description |
|---|---|
| 1 | public 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. |
| 2 | public 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. |
| 3 | public static string Concat(string str0, string str1) Concatenates two string objects. |
| 4 | public static string Concat(string str0, string str1, string str2) Concatenates three string objects. |
| 5 | public static string Concat(string str0, string str1, string str2, string str3) Concatenates four string objects. |
| 6 | public bool Contains(string value) Returns a value indicating whether the specified String object occurs within this string. |
| 7 | public static string Copy(string str) Creates a new String object with the same value as the specified string. |
| 8 | public 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. |
| 9 | public bool EndsWith(string value) Determines whether the end of the string object matches the specified string. |
| 10 | public bool Equals(string value) Determines whether the current String object and the specified String object have the same value. |
| 11 | public 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() ;
}
}
}