實現把文章按空格打散:
public static void Main()
{
string a = "While laughter is is very aerobic activity engages every single organ system";
string b = " ";
string[] c = TestDoWhile.Split(a, b);
foreach (string bb in c)
Console.Write(bb + "\r\n");
}
public static string[] Split(string input, string pattern)
{
string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern);
return arr;
}
(執行效果略,將是一個單字一行行輸出)
3. @"abc" 和 "abc" 區別在哪裡
@"abc" 和 "abc" 沒什麼區別,不過 @ 是忽略轉義字元!
例如 "abc\n" 輸出 abc 並換行;但 @"abc\n" 輸出 abc\n 無換行!
取消轉義
想將 C:\Windows\system 這字串賦值給 str,一般要:
string str = "C:\\Windows\\system";
因為 C# 中 \\ 才轉義成單一 \。又如 \n 是換行符。加入 @ 可寫成:
string str = @"C:\Windows\system";
4. 保留 2 位有效小數及四捨五入
常遇到的問題。
保留 2 位有效小數(保留 N 位參數不同而已):
static void Main()
{
Double a = 12.345678;
Console.Write(Math.Round(a, 2));
}
// 輸出 12.35
四捨五入:
static void Main()
{
Double a = 12.345678; // 要四捨五入的數
Console.Write((a * 10000 + 0.5) / 10000);
}