C# 入門語法整理(字串函數)


常用字串函數

用途
回傳
.Tostring()
其他型別轉字串
字串
char sp = ‘,’;
string someStr=”joe,alisa,tesla”;
string[] array1 =someStr.split(sp)
把字串分割成陣列
字串陣列
.Trim()
清字串末尾的空白
字串
(“where a={0}”, str1)
字串格式化
字串
.replace(“目標字元”,”取代後字元”)
取代某個字串
字串
string.Contains("goodbye")
確認是否包含某個字元
bool
string.IndexOf(“清華”, 0)
查出某字串自第幾個字元起是否包含要查詢的詞彙
Int,表示所在位置,
不存在時,回傳-1
string.Compare("字串", "測試"); // result = -1 
string.Compare("字串測試", "字串測試"); // result = 0 
比對兩個字串是否完全相符
不同=-1
相同=0

.ToUpper()
轉大寫
字串
.ToLower()
轉小寫
字串
.Substring(1, 2)
擷取某部分字串
字串

轉型別(型別寫在前面)
(int32)obj

求餘數
%
Stringbuilder 類別
用於大量的字串串接,提高效能
Stringbuilder sb = new stringbuilder();
Sb.append(“xxxxxxxx”);

條件 (三元) 運算子
Stirng vColor = ( thisClass=”金牌會員”)? “gold”: “gray”;

HTMLENCODE
System.Net.WebUtility.HtmlDecode(texts);


留言

這個網誌中的熱門文章

The Disk2vhd Disaster — and How StarWind’s V2V Brought Redemption! --About Windows Server 2012 STD P2V

如何刪除Trello的卡片資訊 (deleting Trello cards)

Redis 在 C#的應用