1. 程式人生 > 其它 >C#——String類

C#——String類

string型別

構造函式

public string(char[] value)
public string(char[] value,int offset,int count)

拼接字串: + or +=

比較字串

public static int Compare(string strA,string strB)

public static int Compare(string strA,string strB,bool ignoreCase)  //ignoreCase 是否忽略大小寫 

public int CompareTo(Object value)

public int
CompareTo(string value) str1.CompareTo(str2); public bool Equals(string value) str1.Equals(str2); public static bool Equals(string a,string b) string.Equals(str1,str2);

格式化字串

public static string Format(string format,Object argo)

public static string Format(string format,params Object[] args)

Console.WriteLine(
string.Format("{0}{1}{2}{3}",a,A,67,true)); Console.WriteLine("{0}{1}{2}{3}",a,A,67,true);
string.Format("貨幣格式:{0:C}",100);                // ¥100.00
string.Format("十進位制格式:{0:D}",100);             //100
string.Format("指數格式:{0:E3}",100000);          //1.000E+005
string.Format("固定精度格式:{0:F2}",Math.PI);   //3.14
string.Format("逗號分隔千位:{0:N}
",12345678);//12,345,678.00 string.Format("百分比格式:{0:P}",0.5); //50.00% string.Format("十六進位制格式:{0:X}",20); //14
DateTime Date = DateTime.Now; Console.WriteLine(string.Format("短日期格式:{0:d}",Date));    
Console.WriteLine(string.Format("長日期格式:{0:D}",Date));
Console.WriteLine(string.Format("完整日期時間格式:{0:f}",Date));        
Console.WriteLine(string.Format("短時間格式:{0:t}",Date));   
Console.WriteLine(string.Format("長時間格式:{0:T}",Date));
Console.WriteLine(string.Format("月日格式:{0:M}",Date));             
Console.WriteLine(string.Format("年月格式:{0:Y}",Date));
/*輸出
短日期格式:6/18/2021
長日期格式:Friday, June 18, 2021
完整日期時間格式:Friday, June 18, 2021 8:49 AM
短時間格式:8:49 AM
長時間格式:8:49:16 AM
月日格式:June 18
年月格式:June 2021
*/

格式化的另一種方法: Date.ToString("D"); i.ToString("C");

擷取字串

public string Substring(int startIndex)
public string Substring(int StartIndex,int length)   //length 要擷取的字元數
//例.從身份證號中讀取出生年月日
string str = "123456199001010000";
Console.WriteLine(str.Substring(6,8));

分隔字串

public string[] Split(params char[] separator)   //separator 指定分隔符號
public string[] Split(char[] separator,int count)  //count 指定分隔次數
//例.分隔郵箱收件人地址
string str = "[email protected];[email protected];[email protected]";
string[] strArray = str.Split(new char[] {';'},2);
for (int i = 0;i<str.Length;i++)
{
    Console.WriteLine(strArray[i]);
}
/*輸出:
[email protected]
[email protected];[email protected]
*/

插入字串

public string Insert(int startIndex,string value)
str.Insert(5,"World");

刪除字串

public string Remove(int startIndex)
public string Remove(int startIndex,int count)
str.Remove(5,5);

複製字串

public static string Copy(string str)    
public void CopyTo(int sourceIndex,char[] destination,int destinationIndex,int count)  //(起始位置,儲存字串陣列,字元陣列開始儲存位置,複製字元個數)

string strOld = "This is a test.";
char[] strNew = new char[4];
strOld.CopyTo(strOld.IndexOf("test"),strNew,0,4);  //IndexOf 查詢字串中指定字元或字串首次出現的位置,返首索引值

替換字串

public string Replace(char OChar,char NChar)    //替換字元
public string Replace(string OValue,string NValue)   //替換字串

string str = "1個人1頓飯吃1個包子喝1碗粥。";
string str1 = str.Replace('1', '');
string str2 = str.Replace("包子", "雞蛋");
/*輸出:
一個人一頓飯吃一個包子喝一碗粥。
1個人1頓飯吃1個雞蛋喝1碗粥。
*/

可變字串類

using System.Text;
public StringBuilder()
public StringBuilder(int capacify)        //指定容量
public StringBuilder(string value)       //指定初始值
public StringBuilder(int capacity,int maxCapacity)    //起始大小,最大容量
public StringBuilder(string value,int capacity)   //指定初始值和初始大小
public StringBuilder(string value,int startIndex,int length,int capacity)  //指定value中的一部分建立,從startIndex開始,擷取長度為length

//常用方法
strb.Insert(int startIndex,Object value);
strb.Remove(int startIndex,int length);
strb.Replace(char OldChar,char NewChar);
strb.Append(Object value);