.Net C# 字串學習總結

來源:互聯網
上載者:User

以前都是轉寄別人的貼子,今天發一個我的學習筆記.廢話不多說,見下:
(1)比較字串
CompareTo:
str1.CompareTo(str2); 返回int
如果str1長於str2 返回1  如果str2長於str1則返回-1
如果相等,則返回0
Equals:
str1.Equals(str2);  返回bool
如果str1與str2相等 則返回True,否則返回False
(2)定位字元
Int i = str1.Indexof(str2);
在str1中找字元或子串str2並返回索引 索引從"0"開始記數
(3)格式化字串
Format: 將任何數值枚舉日期格式化為具體格式的字串
文法:Public Static String Format(string format,object argo);
第一個參數為:格式    第二個參數為要格的對象
我自己理解為使用者用自訂格式處理資料
(4)截取子串
Substring:
文法:Public String Substring (int first,int length);
索引從first開始截取長度為length的子串
(5)分隔字串
Split:
文法:Public String[] Split (char[] sep);
以參數為分隔點,把字串分割為字串組
例如:
Str1='1|2|3|4|5";
string[] idnum = str1.Split('|');
(6)插入字串
文法:Public String insert (int index,string value);
其中,index為要插入的索引位置,value為要插入的子串。
(7)填充字串
文法:Public String padleft (int total,char padchar);
意為在字串左端填充字元padchar 使填充後的總長度為total  注意:是填充後的長度
同樣,也可以在右面填充,padright
(8)刪除字串及子串
文法:Public String Remove (int index,int count);
Str1.Remove(5,9);
從Str1中,從索引5開始,刪除之後的9個字元。文法中,index為開始刪除字元的索引,count為要刪除的字元數。
(9)移除字串
文法:Public String TrimStart (char[] trimchars );
Str1.trimstart ('%','#');
即從Str1的0索引位置開始,依次向後,刪除字元%和#,如果下一個字元不是%和#則停止。
(10)複製字串
這個基本不用說,Public string copy(string 要複製的字串);
(11)替換字串
文法:Public String Replace ( string old,string new);
參數中,old為要替換掉的子串,new為要替換成的子串。

打了這麼多,希望對大家有協助,也希望大家跟貼補充,一起進步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.