- 正確操作字串
- String str1 = "str1" + 9;
- String str2 = "str2" + 9.ToString(); 2比1好, 不會發生裝箱行為
-
sring s1 = "abc";
s1 = "123" + s1 + "456"; - string s1 = "123" + "abc" + "456"; 2比1好, 1需要在記憶體建立三個字串物件, 還執行string.Contact
-
string a = "t";a += "e";a += "s";a += "t";
string result = a; -
string a = "t";string b = "e";string c = "s";string d = "t";
StringBuilder sb = new StringBuilder(a);
sb.Append(b);sb.Append(c);sb.Append(d);
string result = sb.ToString(); -
string a = "t";string b = "e";string c = "s";string d = "t";
string result = string.Format("{0}{1}{2}{3}", a, b, c, d);
3=2 >>> 1, StringBuilder預設長度為16, 超過才會重新分配記憶體, string.Format內部使用StringBuilder
- 使用預設的轉型方法
2014-06-19
C#完美化.Ch1.基本語言要素
這篇算是讀書心得, 稍微紀錄一下以免忘了
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言