string各种方法的应用
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 测试 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string str = GetSuString(); 14 Console.ReadKey(); 15 } 16 17 public static string GetSuString() 18 { 19 string str = "我是中华人民共和国的一份子"; 20 string str01 = str.Substring(5);// startIndex: 此实例中子字符串的起始字符位置(从零开始)。 21 //结果:共和国的一份子 22 string str02 = str.Substring(2, 5);// 与此实例中在 startIndex 处开头、长度为 length 的子字符串等效的一个字符串,如果 startIndex 等于此实例的长度且 length 23 //结果:中华人民共 24 return str; 25 26 } 27 28 } 29 30 31 32 33 }
数字准确位数
private static void Main(string[] args) { double num1 = 0.56789; string result1 = num1.ToString("#0.0000"); //点后面几个0就保留几位 Console.WriteLine(result1); double num2 = 0.56789; string result2 = num2.ToString("f5"); //fN 保留N位,四舍五入 Console.WriteLine(result2); }
浙公网安备 33010602011771号