C# string方法总结

目录

1、string   null、""、String.Empty的区别

 1.1、""和String.Empty

String.Empty的内部实现:

public static readonly String Empty = "";

所以String.Empty的内部实现是相同于""的,一般使用是可以把这俩化为等号的

1.2、""和null

string对象的值存储在堆上,栈上存储的是值在堆中的地址。

""在堆和栈中都会分配内存。

null只会在栈中分配内存。

2、string方法属性总结

            string str1 = "Ffly";
            string str2 = "f e i ";

            //获取字符串长度
            Console.WriteLine(str1.Length);
            
            //返回指定的字符串第一次出现的位置,没有则返回-1
            Console.WriteLine(str1.IndexOf('f'));
            
            //返回指定的字符串最后一次出现的位置,没有则返回-1
            Console.WriteLine(str1.LastIndexOf('f'));
            
            //判断某个字符串是否以指定的字符串开头
            Console.WriteLine(str1.StartsWith("Ff"));

            //判断某个字符串是否以指定的字符串结尾
            Console.WriteLine(str1.EndsWith("f"));

            //全部转小写
            Console.WriteLine(str1.ToLower());

            //全部转大写
            Console.WriteLine(str1.ToUpper());

            //不带参数则删除开头结尾所有空格
            Console.WriteLine(str2.Trim());
            //删除开头开头结尾所有指定字符
            Console.WriteLine(str2.Trim("fi".ToCharArray()));
            //TrimStart和TrimEnd同理

            //删除指定位置字符
            Console.WriteLine(str2.Remove(2));
            Console.WriteLine(str2.Remove(2,2));

            //拆分字符串
            str2.Split(' ');

            //替换字符串
            str2.Replace("f", "fff");

            //截取字符串
            str2.Substring(2);
            str2.Substring(2,2);

            //字符串插入
            str2.Insert(1, "fff");

 

参考于https://blog.csdn.net/henulwj/article/details/7830615

参考于http://c.biancheng.net/view/2832.html

posted @ 2020-05-12 18:57  Fflyqaq  阅读(329)  评论(0编辑  收藏  举报