C# 字符串方法
csharpprogrammingserver side programming更新于 2025/9/23 5:46:00
String 类有许多方法可帮助您处理字符串对象。下表列出了一些最常用的方法 −
| Sr.No | 方法 &说明 |
|---|---|
| 1 | public static int Compare(string strA, string strB) 比较两个指定的字符串对象并返回一个整数,指示它们在排序顺序中的相对位置。 |
| 2 | public static int Compare(string strA, string strB, bool ignoreCase ) 比较两个指定的字符串对象并返回一个整数,指示它们在排序顺序中的相对位置。但是,如果布尔参数为真,它将忽略大小写。 |
| 3 | public static string Concat(string str0, string str1) 连接两个字符串对象。 |
| 4 | public static string Concat(string str0, string str1, string str2) 连接三个字符串对象。 |
| 5 | public static string Concat(string str0, string str1, string str2, string str3) 连接四个字符串对象。 |
| 6 | public bool Contains(string value) 返回一个值,指示指定的 String 对象是否出现在此字符串。 |
| 7 | public static string 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 对象是否具有相同的值。 |
让我们看一个使用 C# 中的 Contains() 方法的示例。Contains(string value) 返回一个值,指示指定的 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() ;
}
}
}

