String类具有许多可帮助您处理字符串对象的方法。下表列出了一些最常用的方法-
| 序号 | 方法与说明 |
|---|---|
| 1 | public static int Compare(string strA,string strB) 比较两个指定的字符串对象,并返回一个整数,该整数指示其在排序顺序中的相对位置。 |
| 2 | public static int Compare(string strA,string strB,bool ignoreCase) 比较两个指定的字符串对象,并返回一个整数,该整数指示其在排序顺序中的相对位置。但是,如果布尔参数为true,则忽略大小写。 |
| 3 | 公共静态字符串Concat(string str0,string str1) 连接两个字符串对象。 |
| 4 | 公共静态字符串Concat(字符串str0,字符串str1,字符串str2) 连接三个字符串对象。 |
| 5 | 公共静态字符串Concat(字符串str0,字符串str1,字符串str2,字符串str3) 连接四个字符串对象。 |
| 6 | public bool Contains(string value) 返回一个值,该值指示指定的String对象是否出现在此字符串中。 |
| 7 | 公共静态字符串Copy(string str) 创建一个与指定字符串具有相同值的新String对象。 |
| 8 | public void CopyTo(int sourceIndex,char [] destination,int destinationIndex,int count) 将指定数量的字符从String对象的指定位置复制到Unicode字符数组中的指定位置。 |
| 9 | public bool EndsWith(string value) 确定字符串对象的结尾是否与指定的字符串匹配。 |
| 10 | public bool Equals(string value) 确定当前String对象和指定的String对象是否具有相同的值。 |
| 11 | public static bool Equals(string a,string b) 确定两个指定的String对象是否具有相同的值。 |
让我们看一个使用Contains()C#中的方法的示例。Contains(字符串值)返回一个值,该值指示指定的String对象是否出现在此字符串中。
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() ;
}
}
}