C#中字符串一些使用方法

来源:互联网 发布:加工中心编程入门 编辑:程序博客网 时间:2024/05/16 14:34
<span style="font-family:SimSun;"> <span style="font-size:18px;">         </span><span style="font-size:14px;">                                                      (1)字符串是只读的,不能改变里面的值;   (2)字符串多次赋值以最后一次赋值为字符串的值;   (3)string.IsNullOrEmpty判断 "";string.Empty;null 为空;   (4)字符串比较是否为同一对象使用ReferenceEquals(object A,object B),字符串相等可使用==或str1.Equals(str2)      例: string s1="a"; string s2="b";string s3="c"; string s4=s1+s2+s3; string s5="abc"; string s6="a"+"b"+"c";      其中: s4与s5为不同对象,s5与s6为同一对象;因为s1、s2、s3、开辟了新的空间(为变量);s5与s6本质相同;   (5)IndexOf主要判断字符串是否包含特定内容,没有找到返回为-1;   (6)Substring:str.Substring(3)从索引为3字符截取到最后;str.Substring(1,3);从索引1截取3个字符;   (7)str.Split('|'),截取字符串内部特定的内容;       str.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries)截取字符串内部特定内容去掉空格(char可改为string);   (8)string.Join("-", str)可以将字符串数组使用-连接起来   (9)string.Format("{0}{1}{2}","12","23","45");ADO.NET中使用可能产生SQL注入;   (10)str.Replace("要修改的内容", "修改后的内容"),用与修改字符串中内容;</span></span>

字符串反序:

<span style="font-family:SimSun;">    <span style="white-space:pre"></span>(1) private static string GetStringRevers(string str)        {            char[] chs = str.ToCharArray();            for (int i = 0; i < chs.Length/2; i++)            {                 char temp = chs[i];                chs[i] = chs[chs.Length - 1 - i];                chs[chs.Length - 1 - i] = temp;            }            return new string(chs);        }(2) static void Main(string[] args)        {    string str = Console.ReadLine();            for (int i = str.Length - 1; i >= 0; i--)//倒序for循环            {                Console.WriteLine(str[i]);//结果为char            } }</span>


0 0
原创粉丝点击