1. Convert.ToInt32 與 Int32.Parse 的恩恩怨怨
2. Split 的 3 種用法
3. @"abc" 和 "abc" 區別在哪裡
4. 保留 2 位有效小數及四捨五入
5. URL 傳遞中文解決方案
6. 把 123456789 轉換為 12-345-6789 的 3 種方法
7. 交換兩個指定位置字元的 4 種方法
8. %10 的妙用
9. 輸出 21 個 A 的巧妙做法
1. Convert.ToInt32 與 Int32.Parse 的恩恩怨怨
這 2 個方法都能把字串解析為 int,一定會有疑問:到底有什麼區別?什麼時候用什麼?效能如何?
其實在 2.0 裡還有 Int32.TryParse 也實現同樣效果。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string myString = "1234";
int myint = 0;
myint = Convert.ToInt32(myString);
Console.Write(myint + "\r\n ");
myint = Int32.Parse(myString);
Console.Write(myint + "\r\n ");
Int32.TryParse(myString, out myint);
Console.Write(myint + "\r\n");
}
}
}
(執行效果將是毫無差別,圖略)