manipulación de cadenas
-
- obtener la longitud de la cadena
- interceptar
- separado
- índice de coincidencia
- Truncado hasta la cola (Recortar)
- reemplazar
- conversión de casos
- Inserción y eliminación (Insertar y Eliminar)
- cadena a código de bits
- es un espacio
- Si la cadena es puntuación
- La diferencia entre Int32.Parse() e Int32.TryParse() y Convert.ToInt32()
obtener la longitud de la cadena
int strLength = str.Length;
interceptar
str ="asdf";
str.Substring(1); // 从第2位开始(索引从0开始)截取一直到字符串结束,输出"sdf"
str.Substring(1, 2); // 从第2位开始截取2位,输出"sd"
separado
string str="aaa,bbb,ccc";
string[] sArray = str.Split(',');
índice de coincidencia
str = "ABCABCD";
str.IndexOf('A'); // 从字符串头部开始搜索第一个匹配字符A的位置索引,输出"0"
str.IndexOf("BCD"); // 从字符串头部开始搜索第一个匹配字符串BCD的位置,输出"4"
str.LastIndexOf('C'); // 从字符串尾部开始搜索第一个匹配字符C的位置,输出"5"
str.LastIndexOf("AB"); // 从字符串尾部开始搜索第一个匹配字符串BCD的位置,输出"3"
str.IndexOf('E'); // 从字符串头部开始搜索第一个匹配字符串E的位置,没有匹配输出"-1";
str.Contains("ABCD"); // 判断字符串中是否存在另一个字符串"ABCD",输出true
Truncado hasta la cola (Recortar)
string str = "__AB__CD__";
str.Trim('_'); // 移除字符串中头部和尾部的'_'字符,输出"AB__CD"
str.TrimStart('_'); // 移除字符串中头部的'_'字符,输出"AB__CD__"
str.TrimEnd('_'); // 移除字符串中尾部的'_'字符,输出"__AB__CD"
reemplazar
string str="你好da";
str = str.Replace("你","他");
str = str.Replace('a', 'b');
conversión de casos
str.ToLower(); // 转化为小写
str.ToUpper(); // 转化为大写
Inserción y eliminación (Insertar y Eliminar)
str = "ADEF";
str.Insert(1, "BC"); // 在字符串的第2位处插入字符串"BC",输出"ABCDEF"
str.Remove(1); // 从字符串的第2位开始到最后的字符都删除,输出"A"
str.Remove(0, 2); // 从字符串的第1位开始删除2个字符,输出"EF"
cadena a código de bits
byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
es un espacio
IsWhiteSpace(字符串变量,位数)
char.IsWhiteSpace(str, 3);
Si la cadena es puntuación
char.IsPunctuation('A');
La diferencia entre Int32.Parse() e Int32.TryParse() y Convert.ToInt32()
//Int32.TryParse()性能优于Convert.ToInt32()优于Int32.Parse()。
//他们都是将字符串强制转换为int32类型,当要转换的字符串为非空字符时他们三者的效果相同;
//但要转换的字符串为null时会有区别如下实例
string strint = null;
int num = 0;
Int32.Parse(strint);//会抛出异常;
Convert.ToInt32(strint);//不会抛出异常,返回值为0;
Int32.TryParse(strint, out num);//不会抛出异常,返回值为0,但当转换正确时返回的是true,否则返回false。