码迷,mamicode.com
首页 > Windows程序 > 详细

C# string总结

时间:2020-05-12 20:04:56      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:删除   console   拆分   start   截取   技术   spl   png   返回   

目录

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

C# string总结

标签:删除   console   拆分   start   截取   技术   spl   png   返回   

原文地址:https://www.cnblogs.com/Fflyqaq/p/12878041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!