[C#] C# 将字符串转换成整形、double或者date →→→→→进入此内容的聊天室

来自 , 2019-07-24, 写在 C#, 查看 110 次.
URL http://www.code666.cn/view/fa28c6cd
  1. using System;
  2. class Parse {
  3. /// <summary>
  4. /// Example of various Parse methods
  5. /// </summary>
  6. private static void Main() {
  7.  
  8.     //all the types in .Net have a Parse method
  9.     int i = int.Parse("34");
  10.     bool b = bool.Parse("true");
  11.     double d = double.Parse("234.333");
  12.     DateTime dateTime = DateTime.Parse("2008-03-02 12:20am");
  13.    
  14.     //TryParse will return false if the parsing fails
  15.     int intvalue;
  16.     bool ok = int.TryParse("42", out intvalue); //ok = True, intvalue = 42
  17.     ok = int.TryParse("abc", out intvalue); //ok = False, intvalue = 0
  18.    
  19.     //ParseExtact takes a format
  20.     DateTime dt = DateTime.ParseExact("02-03-2008", "dd-MM-yyyy",
  21.         System.Globalization.CultureInfo.CurrentCulture); //dt = 3/2/2008 12:00:00 AM
  22.  
  23.     Console.Out.WriteLine("dt = " + dt);
  24.     Console.In.ReadLine();
  25. }
  26. }
  27. //csharp/4828

回复 "C# 将字符串转换成整形、double或者date"

这儿你可以回复上面这条便签

captcha