1.C# string api
Substring(),//截取字符串,接受两个参数,第一个是偏移量,第二个是截取长度
replace(),//替换字符串,接受两个参数,第一个指定被替换的字符串,第二个指定被替换的字符串
IndexOf(),//返回指定字符在字符串中的位置,从0开始
//一个重载ToString()方法案例 //Format将double转换为string struct Location { private double x; private double y; public Location(double x,double y) { this.x = x; this.y = y; } public override string ToString() { return System.String.Format("Location:{0}degree,latitude:{1}degree", x, y); } }
//一个访问控制字符 readonly //常用在定义常量,与const 的区别在于,readonly在程序运行时被赋值,赋值之后程序运行中不能更改值
比较两个未本地化字符串的最好方法是使用具有 StringComparison.Ordinal(是否全等) 和 StringComparison.OrdinalIgnoreCase(忽略大小写) 的 Equals 方法。
例如:
bool result = root.Equals(root2, StringComparison.Ordinal); System.Console.WriteLine("{0} and {1},is{2}", root, root2, result ? ",equel." : ",not equel."); result = root.Equals(root2, StringComparison.OrdinalIgnoreCase);//忽略大小写 System.Console.WriteLine("{0}and{1},is{2}", root, root2, result ? ",equel." : ",not equel."); //静态方法也可用 bool isEquel = String.Equals(root, root2, StringComparison.Ordinal); System.Console.WriteLine(isEquel.ToString());
CompareTo() //用于根据一个字符串是小于 (<) 还是大于 (>) 另一个字符串来返回一个整数值。比较字符串时使用的是 Unicode 值,并且小写的值小于大写的值。
Split(),//Split方法将字符串拆分成带分隔符的char数组
例如:
string numString = "1287543"; //"1287543.0" will return false for a long long number1 = 0; bool canConvert = long.TryParse(numString, out number1); if (canConvert == true) Console.WriteLine("number1 now = {0}", number1); else Console.WriteLine("numString is not a valid long"); byte number2 = 0; numString = "255"; // A value of 256 will return false canConvert = byte.TryParse(numString, out number2); if (canConvert == true) Console.WriteLine("number2 now = {0}", number2); else Console.WriteLine("numString is not a valid byte"); decimal number3 = 0; numString = "27.3"; //"27" is also a valid decimal canConvert = decimal.TryParse(numString, out number3); if (canConvert == true) Console.WriteLine("number3 now = {0}", number3); else Console.WriteLine("number3 is not a valid decimal");
long的tryParse()方法:将数字转换为64位有符号整型,返回转换是否成功;参数:
string numString="123456"; long number=0; bool canConvert=long.tryParse(numString,number);//返回是否成功将numString转换为long类型的number成功