一、字符串操作
1. 字符串连接
//将指定的数组所有元素拼接为一个字符串 string[] arr = {"A","B","C"}; string.Concat(arr); //使用连字符将数组拼接为字符串 string.Join(",",arr); //输出"A,B,C"
2. 提取字符串中的字符
//获取字符串中的某个char 字符 string str = "Hello"; char ch = str[4]; //ch = ‘o‘ //字符串截取 顾头不顾尾 从0开始 string newStr = str.Substring(0,1); //newStr = "H"
3. 字符串大小写转换
string str = "Abc"; //大写 str.ToUpper(); //ABC //小写 str.ToLower //abc
4. 拆分字符串为字符串数组
string str = "A,B,C"; string[] arr = str.Split(‘,‘); //arr {"A","B","C"}
5. 字符替换
//将str的"_"替换为"," string str = "A_B_C"; str.Replace(‘_‘,","); //str = "A,B,C"
6. 去除字符串首尾空格
string str = "A B C"; string newStr = str.Trim(); //newStr = "ABC"
7. 匹配索引
string str = "ABCDE"; int index = str.IndexOf(‘A‘); //index = 0
8. 字符串填充,补齐
string str = "666"; //左填充 str.PadLeft(6,‘0‘); //返回"000666" //右填充 str.PadRight(6,‘0‘); //返回"666000"
9. 字符串格式化(静态方法)
string str = string.Format("{0}---{1}",pig,dog); //str = "pig---dog"
10. 判断字符串是否包含指定字符
string a = "I am Mike"; string b = "Mike"; bool result = a.Contains(b); //result = true
二、常用值类型转换
1. int 类型转换为 string
int a = 250; string b = a.ToString(); //b = "250"
2. string 转换为 int
string a = "250"; int result; //方法1 result = int.Parse(a); //方法2 result = Convert.ToInt32(a); //方法3 int.TryParse(a,out result);