马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
字符串属性
- string str = "打工人";
- Console.WriteLine(str);
- char s = str[0];
- Console.WriteLine(s);
复制代码 字符串内置API(函数)
1. Concat 拼接字符串
- string s1 = "打";
- string s2 = "工";
- string s3 = "人";
- string sth=string.Concat(s1, s2, s3);
- Console.WriteLine(sth);//打工人
复制代码 2.Containts 判断字符是否包含
- bool ch=sth.Contains("人");
- Console.WriteLine(ch);//true
复制代码 3.CopyTo 复制字符串
- char[] chars = new char[10];
- sth.CopyTo(1,chars,0,2);
- Console.WriteLine(chars);//工人
复制代码 4. char转字符串
- string charstr=new string(chars);
- Console.WriteLine(charstr);//工人
复制代码 5. ToUpper 小写转大写
- string abc = "aBcD";
- Console.WriteLine(abc.ToUpper());//ABCD
复制代码 6. ToLower 大写转小写
- Console.WriteLine(abc.ToLower());//abcd
复制代码 7. Replace 替换关键字
- string sth1 = "打工人打工魂";
- Console.WriteLine(sth1.Replace("人","people"));//打工人people打工魂
- Console.WriteLine(sth1.Replace("打工魂",""));//打工人
复制代码 8. StartsWith 是否以..开头
- string name = "立讯机器人";
- Console.WriteLine(name.StartsWith("立"));//True
复制代码 9. EndsWith 以...结尾
- Console.WriteLine(name.EndsWith("人"));//True
复制代码 10. Equals 是否相等
- Console.WriteLine(name.Equals("立讯机器人"));
- string c1 = "123";
- string c2 = "123";
- Console.WriteLine(string.Equals(c1,c2));//True
- object obj1=new object();
- object obj2=new object();
- Console.WriteLine(object.Equals(obj1,obj2));//比较引用类型 False
复制代码 11. IndexOF 从前往后查询 初次在源字符串中出现的索引位置
- string num = "ABCc123c";
- Console.WriteLine(num.IndexOf('a'));//-1
- Console.WriteLine(num.IndexOf('A'));//0
- Console.WriteLine(num.IndexOf('c'));//3
- //12.StringComparison.OrdinalIgnoreCase() 忽略大小写进行查询
- Console.WriteLine(num.IndexOf("c",StringComparison.OrdinalIgnoreCase));//2
- Console.WriteLine(num.IndexOf("c",5));//从5的位置开始查询 7
复制代码 12.StringComparison.OrdinalIgnoreCase() 忽略大小写进行查询
- Console.WriteLine(num.IndexOf("c",StringComparison.OrdinalIgnoreCase));//2
- Console.WriteLine(num.IndexOf("c",5));//从5的位置开始查询 7
复制代码 13. LastIndexOf() 从后向前查询初次在源字符串中出现的索引值位置
- Console.WriteLine(num.LastIndexOf("a"));//-1
复制代码 14. IndexOfAny() 从前往后查询初次出现的指定字符数组中任意一个
- Console.WriteLine(num.IndexOfAny(new char[] {'a','A','b','c'}));//0
复制代码 15.IsNullOrEmpty() 判断参数字符串是否为 "" null Empty
- string num1 = "";
- string num2 = null;
- string num3=string.Empty;
- Console.WriteLine(string.IsNullOrEmpty(num1));//True
- Console.WriteLine(string.IsNullOrEmpty(num2));//True
- Console.WriteLine(string.IsNullOrEmpty(num3));//True
复制代码 16. Insert() 在指定的位置插入字符串 天生新的字符串
- string ss = "abc";
- Console.WriteLine(ss.Insert(1,"w"));//awbc
复制代码 17. Join() 拼接字符串
- char[] sw1 = new char[] {'a','b','c'};
- string [] sw2 = new string[] {"aa","bb","cc"};
- Console.WriteLine(string.Join("+",sw1));//a+b+c
- Console.WriteLine(string.Join("-",sw2));//aa-bb-cc
复制代码 18. Remove() 删除
- Console.WriteLine(name.Remove(4));//立讯机器
- Console.WriteLine(name.Remove(2,2));//立讯人
复制代码 19. Split() 将字符串分割成字符串数组
- string sum = "张三,李四,王五,赵六";
- Console.WriteLine(sum.Split(','));
- string[] sss = sum.Split(',');
复制代码 20.将字符串转换为字符数组
- char[]char1=sum.ToCharArray();
- for(int i=0;i<char1.Length;i++)
- {
- Console.WriteLine(char1[i]);
- }
复制代码 21. Substring() 截取字符串
- Console.WriteLine(sum.Substring(1,1));//三
复制代码 22. StringBuilder() 创建字符串
- StringBuilder sb = new StringBuilder();
- sb.Append("你好啊!");
- Console.WriteLine(sb);//你好啊!
- Console.WriteLine(sb.Length);//4
- Console.WriteLine(sb.Remove(1,2));//你!
- Console.WriteLine(sb.Replace("你","*"));//*!
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |