在C#编程语言中,String类是用于处理文本字符串的核心类。它提供了丰富的功能,使得在处理字符串时能够实现各种常见的操作。以下是对C#中String类常用方法的详细解释和示例: 1. `.ToLower()` 和 `.ToUpper()` 这两个方法用于将字符串转换为小写或大写形式,方便进行大小写的统一处理。 ```csharp string str = "AbC"; string lowerStr = str.ToLower(); // "abc" string upperStr = str.ToUpper(); // "ABC" ``` 2. `.Trim()` `.Trim()` 方法用于移除字符串两端的空白字符,如空格、制表符等。 ```csharp string trimmedStr = " abc ".Trim(); // "abc" ``` 3. `.Equals(string value, StringComparison comparisonType)` 此方法用于比较两个字符串是否相等,`comparisonType` 参数允许你选择不同的比较规则,如: - `StringComparison.CurrentCulture`: 根据当前区域设置进行比较。 - `StringComparison.OrdinalIgnoreCase`: 忽略大小写和区域设置进行比较。 ```csharp string str1 = "abc"; string str2 = "ABC"; bool equals = str1.Equals(str2, StringComparison.OrdinalIgnoreCase); // true ``` 4. `.CompareTo(string value)` `.CompareTo` 方法用于比较当前字符串与另一个字符串的顺序。返回值表示比较结果,0 表示两者相等,负数表示当前字符串小于对方,正数表示当前字符串大于对方。 ```csharp string str1 = "apple"; string str2 = "banana"; int result = str1.CompareTo(str2); // -1 ``` 5. `.Split(params char[] separator)` `.Split` 方法用于根据指定的分隔符将字符串分割成多个子字符串,返回一个字符串数组。 ```csharp string str = "one,two,three"; string[] parts = str.Split(','); // {"one", "two", "three"} ``` 6. `.Replace(char oldChar, char newChar)` 和 `.Replace(string oldStr, string newStr)` 这两个方法用于在字符串中查找并替换指定的字符或子字符串。 ```csharp string str = "hello world"; string replacedStr = str.Replace('o', 'x'); // "hexxl xlwrld" string replacedWord = str.Replace("world", "universe"); // "hello universe" ``` 7. `.Substring(int startIndex)` 和 `.Substring(int startIndex, int length)` 这两个方法用于获取字符串的子串。第一个版本从指定的索引开始到字符串末尾,第二个版本则指定起始索引和长度。 ```csharp string str = "hello"; string subStr1 = str.Substring(1); // "ello" string subStr2 = str.Substring(1, 3); // "ell" ``` 8. `.Contains(char c)`、`.Contains(string str)`、`.StartsWith(string str)` 和 `.EndsWith(string str)` 这四个方法分别用于检查字符串是否包含某个字符、子字符串,或者是否以某个子字符串开始或结束。 ```csharp string str = "Hello, World!"; bool hasComma = str.Contains(','); // true bool startsWithHello = str.StartsWith("Hello"); // true bool endsWithWorld = str.EndsWith("World!"); // true ``` 9. `.IndexOf(char c)` 和 `.IndexOf(string str)` 这两个方法用于查找字符或子字符串在字符串中的位置,返回其索引,找不到则返回 -1。 ```csharp string str = "Hello, World!"; int commaIndex = str.IndexOf(','); // 5 int worldIndex = str.IndexOf("World"); // 7 ``` 通过理解和熟练使用这些方法,你可以高效地处理C#中的字符串,提高代码的可读性和性能。在实际开发中,根据需求选择合适的方法组合,可以轻松完成各种复杂的字符串操作。
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/release/download_crawler_static/12807686/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 5
- 资源: 899
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)