文字列関連の操作の概要

文字列の長さを取得する

int strLength = str.Length;  

インターセプト

str ="asdf";
str.Substring(1); // 从第2位开始(索引从0开始)截取一直到字符串结束,输出"sdf"
str.Substring(1, 2); // 从第2位开始截取2位,输出"sd"

string str="aaa,bbb,ccc";
string[] sArray = str.Split(',');

一致インデックス

        str = "ABCABCD";
        str.IndexOf('A'); // 从字符串头部开始搜索第一个匹配字符A的位置索引,输出"0"
        str.IndexOf("BCD"); // 从字符串头部开始搜索第一个匹配字符串BCD的位置,输出"4"
        str.LastIndexOf('C'); // 从字符串尾部开始搜索第一个匹配字符C的位置,输出"5"
        str.LastIndexOf("AB"); // 从字符串尾部开始搜索第一个匹配字符串BCD的位置,输出"3"
        str.IndexOf('E'); // 从字符串头部开始搜索第一个匹配字符串E的位置,没有匹配输出"-1";
        str.Contains("ABCD"); // 判断字符串中是否存在另一个字符串"ABCD",输出true

尾部まで切り詰め(トリム)

        string str = "__AB__CD__";
        str.Trim('_'); // 移除字符串中头部和尾部的'_'字符,输出"AB__CD"
        str.TrimStart('_'); // 移除字符串中头部的'_'字符,输出"AB__CD__"
        str.TrimEnd('_'); // 移除字符串中尾部的'_'字符,输出"__AB__CD"

交換する

        string str="你好da";
        str = str.Replace("你","他");
        str = str.Replace('a', 'b');

大文字と小文字の変換

        str.ToLower(); // 转化为小写
        str.ToUpper(); // 转化为大写

挿入と削除 (Insert and Remove)

        str = "ADEF";
        str.Insert(1, "BC"); // 在字符串的第2位处插入字符串"BC",输出"ABCDEF"
        str.Remove(1); // 从字符串的第2位开始到最后的字符都删除,输出"A"
        str.Remove(0, 2); // 从字符串的第1位开始删除2个字符,输出"EF"

文字列をビットコードに変換

byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
        

スペースですか

IsWhiteSpace(字符串变量,位数)
 char.IsWhiteSpace(str, 3);

文字列が句読点であるかどうか

        char.IsPunctuation('A');

Int32.Parse() と Int32.TryParse() および Convert.ToInt32() の違い

         //Int32.TryParse()性能优于Convert.ToInt32()优于Int32.Parse()。
        //他们都是将字符串强制转换为int32类型,当要转换的字符串为非空字符时他们三者的效果相同;
        //但要转换的字符串为null时会有区别如下实例
        string strint = null;
        int num = 0;
        Int32.Parse(strint);//会抛出异常;
        Convert.ToInt32(strint);//不会抛出异常,返回值为0;
        Int32.TryParse(strint, out num);//不会抛出异常,返回值为0,但当转换正确时返回的是true,否则返回false。
       

おすすめ

転載: blog.csdn.net/m0_54765221/article/details/126598001