常用方法列表
  • ToCharArray()将字符串转为char类型数组
  • new string(char[] chs)将字符数组转为字符串
  • Length获取字符串长度
  • ToUpper()字符串转为首字母大写
  • ToLower()将字符串首字母转为小写
  • Equals, 判断两个字符串是否相等,默认区分大小写,第二个参数StringComparison.OrdinalIgnoreCase可以忽略大小写
  • Split字符串分割 ,StringSplitOptions.RemoveEmptyEntries 移除空元素
  • Contains() 字符串中是否包含某个字符串
  • Substring(int startIndex)取从位置startIndex开始一直到最后的子字符串,Substring(int startIndex, int length) 取从位置startIndex开始,长度为length的字符串
  • StartsWith(string value) 判断字符串是否以子字符串value开始
  • EndsWith(string value) 判断字符串是否以子字符粗value结尾
  • IndexOf(string value) 取子字符串value第一次出现的位置
  • LastIndexOf()取子字符串value最后一次出现的位置
  • Trim() 去掉字符串两边的空格
  • TrimStart() 去掉字符串左侧空格
  • TrimEnd() 去掉字符串右侧空格
  • IsNullOrEmpty() 判断字符串是否为空
  • Join()将数组串联为字符串
代码示例
using System;

using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "hello";
            // ToCharArray()将字符串转为char类型数组
            char[] chs = s.ToCharArray();
            chs[0] = 'b';
            Console.WriteLine(chs);
            // new string(char[] chs)将字符数组转为字符串
            string s2 = new string(chs);
            Console.WriteLine(s2);
            // Length获取字符串长度
            Console.WriteLine(s2.Length);
            // ToUpper()字符串转为首字母大写
            Console.WriteLine(s2.ToUpper());
            // ToLower()将字符串首字母转为小写
            Console.WriteLine(s2.ToLower());
            // 判断两个字符串是否相等,Equals, 默认区分大小写,第二个参数StringComparison.OrdinalIgnoreCase可以忽略大小写
            Console.WriteLine(s2.Equals(s2.ToUpper()));
            Console.WriteLine(s2.Equals(s2.ToUpper(), StringComparison.OrdinalIgnoreCase));

            string s3 = "a b fds _ +,= ";
            char[] chars = {' ','_','+','=', ','};
            // Split字符串分割 ,StringSplitOptions.RemoveEmptyEntries 移除空元素
            string[] strArr = s3.Split(chars, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine(strArr.Length);

                  // Contains() 字符串中是否包含某个字符串
            string str4 = "敏感词";
            if (str4.Contains("敏感"))
            {
                str4 = str4.Replace("敏感", "**");
            }
            Console.WriteLine(str4);

            string str5 = "今天天气好晴朗,处处好风光";
            // Substring(int startIndex)取从位置startIndex开始一直到最后的子字符串
            // Substring(int startIndex, int length) 取从位置startIndex开始,长度为length的字符串
            str5 = str5.Substring(1, 2);
            Console.WriteLine(str5);

            // StartsWith(string value) 判断字符串是否以子字符串value开始
            string str6 = "Hello";
            if (str6.StartsWith("H"))
            {
                Console.WriteLine("Yes start with h");
            }
            else
            {
                Console.WriteLine("No start with h");
            }

            // EndsWith(string value) 判断字符串是否以子字符粗value结尾
            if (str6.EndsWith("o"))
            {
                Console.WriteLine("Yes end with o");
            }
            else
            {
                Console.WriteLine("No end with o");
            }

            // IndexOf(string value) 取子字符串value第一次出现的位置
            int startIndex = str6.IndexOf("l");
            Console.WriteLine(startIndex);

            // LastIndexOf()取子字符串value最后一次出现的位置
            int lastIndex = str6.LastIndexOf("l");
            Console.WriteLine(lastIndex);


            // Trim() 去掉字符串两边的空格
            // TrimStart() 去掉字符串左侧空格
            // TrimEnd() 去掉字符串右侧空格

            string str7 = "  hello c#  ";
            Console.WriteLine(str7.Trim());
            Console.WriteLine(str7.TrimStart());
            Console.WriteLine(str7.TrimEnd());
         
            // IsNullOrEmpty() 判断字符串是否为空
            string str8 = null;
            if (string.IsNullOrEmpty(str8))
            {
                Console.WriteLine("Yes null");
            }
          
            // Join()将数组串联为字符串
            string[] lan = {"php", "c", "c#", "java", "golang", "python" };
            string joinStr = string.Join("、", lan);
            Console.WriteLine(joinStr);

            Console.ReadKey();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.