C#中 String类API(函数)

字符串属性

cs 复制代码
 string str = "打工人";
 Console.WriteLine(str);
 char s = str[0];
 Console.WriteLine(s);

字符串内置API(函数)

1. Concat 拼接字符串

cs 复制代码
 string s1 = "打";
 string s2 = "工";
 string s3 = "人";
 string sth=string.Concat(s1, s2, s3);
 Console.WriteLine(sth);//打工人

2.Containts 判断字符是否包含

cs 复制代码
bool ch=sth.Contains("人");
Console.WriteLine(ch);//true

3.CopyTo 复制字符串

cs 复制代码
 char[] chars = new char[10];
 sth.CopyTo(1,chars,0,2);
 Console.WriteLine(chars);//工人

4. char转字符串

cs 复制代码
string charstr=new string(chars);
Console.WriteLine(charstr);//工人

5. ToUpper 小写转大写

cs 复制代码
string abc = "aBcD";
Console.WriteLine(abc.ToUpper());//ABCD

6. ToLower 大写转小写

cs 复制代码
Console.WriteLine(abc.ToLower());//abcd

7. Replace 替换关键字

cs 复制代码
string sth1 = "打工人打工魂";
Console.WriteLine(sth1.Replace("人","people"));//打工人people打工魂
Console.WriteLine(sth1.Replace("打工魂",""));//打工人

8. StartsWith 是否以..开头

cs 复制代码
string name = "立讯机器人";
Console.WriteLine(name.StartsWith("立"));//True

9. EndsWith 以...结尾

cs 复制代码
Console.WriteLine(name.EndsWith("人"));//True

10. Equals 是否相等

cs 复制代码
 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 从前往后查询 首次在源字符串中出现的索引位置

cs 复制代码
 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() 忽略大小写进行查询

cs 复制代码
Console.WriteLine(num.IndexOf("c",StringComparison.OrdinalIgnoreCase));//2
Console.WriteLine(num.IndexOf("c",5));//从5的位置开始查询   7

13. LastIndexOf() 从后向前查询首次在源字符串中出现的索引值位置

cs 复制代码
Console.WriteLine(num.LastIndexOf("a"));//-1

14. IndexOfAny() 从前往后查询首次出现的指定字符数组中任意一个

cs 复制代码
Console.WriteLine(num.IndexOfAny(new char[] {'a','A','b','c'}));//0

15.IsNullOrEmpty() 判断参数字符串是否为 "" null Empty

cs 复制代码
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() 在指定的位置插入字符串 生成新的字符串

cs 复制代码
string ss = "abc";
Console.WriteLine(ss.Insert(1,"w"));//awbc

17. Join() 拼接字符串

cs 复制代码
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() 删除

cs 复制代码
 Console.WriteLine(name.Remove(4));//立讯机器
 Console.WriteLine(name.Remove(2,2));//立讯人

19. Split() 将字符串分割成字符串数组

cs 复制代码
string sum = "张三,李四,王五,赵六";
Console.WriteLine(sum.Split(','));
string[] sss = sum.Split(',');

20.将字符串转换为字符数组

cs 复制代码
 char[]char1=sum.ToCharArray();
 for(int i=0;i<char1.Length;i++)
 {
     Console.WriteLine(char1[i]);
 }

21. Substring() 截取字符串

cs 复制代码
Console.WriteLine(sum.Substring(1,1));//三

22. StringBuilder() 创建字符串

cs 复制代码
 StringBuilder sb = new StringBuilder();
 sb.Append("你好啊!");
 Console.WriteLine(sb);//你好啊!
 Console.WriteLine(sb.Length);//4
 Console.WriteLine(sb.Remove(1,2));//你!
 Console.WriteLine(sb.Replace("你","*"));//*!
相关推荐
YKPG几秒前
c语言修炼秘籍 - - 禁(进)忌(阶)秘(技)术(巧)【第七式】程序的编译
linux·c语言·开发语言
TNTLWT1 小时前
程序生成随机数
开发语言
ansenXia1 小时前
CentOS系统中排查进程异常终止的日志
开发语言·python
ᖰ・◡・ᖳ2 小时前
前端之勇闯DOM关
开发语言·前端·javascript·学习·html
双叶8362 小时前
(51单片机)LCD显示温度(DS18B20教程)(LCD1602教程)(延时函数教程)(单总线教程)
c语言·开发语言·单片机·嵌入式硬件·mongodb·51单片机·nosql
好蛋编程2 小时前
【c语言】指针进阶
c语言·开发语言
二猛子2 小时前
Qt-托盘的实现
开发语言·qt
冰茶_3 小时前
WPF特性分析
学习·microsoft·c#·wpf
未来之窗软件服务3 小时前
眼镜眨巴眨巴-一步几个脚印从头设计数字生命——仙盟创梦IDE
开发语言·ide·人工智能·php·仙盟创梦ide
刚入坑的新人编程3 小时前
C++继承(最详细)
开发语言·c++