C#字符串常見處理

以下是我收集的關於常見字符串處理中易誤解的地方:

時刻應該記住的是:字符串是引用變量,且在做任何操作後其本身的值保持不變,如要改變其本身的值,則還應對自身重新賦值

 

string firstName = "Miracle";
string lastName = "He";
StringBuilder sb = new StringBuilder();
sb.Append(firstName);
sb.Append(lastName);
string name = sb.ToString();//比"+"性能更好
string str = name.Substring(1, 2);
//該方法簽名Substring(startIndex, count)其中第二個參數爲截取位數而不是所謂的結束索引endIndex
Console.WriteLine(str);//輸出ir
byte[] byteStr = System.Text.Encoding.Default.GetBytes(name);
Console.WriteLine(byteStr.Length);//輸出9
string fullName = "Miracle He";
Console.WriteLine(Char.IsWhiteSpace(fullName, 7));//是否爲空白:輸出true
Console.WriteLine(Char.IsPunctuation(fullName, 1));//是否爲標點:輸出false
Console.WriteLine((int)'');//轉化爲對應的ASCII碼值: 20309
Console.WriteLine((char)20309);//轉化爲對應的字符: 何

//刪除字符串的最後一個字符
string testStr = "Miracle He";
Console.WriteLine(testStr.Substring(0, testStr.Length - 1));//輸出Miracle H
Console.WriteLine(testStr.TrimEnd('e'));//輸出Miracle H
Console.WriteLine(testStr.TrimEnd(new char[] { 'e' }));//輸出Miracle H
Console.WriteLine(testStr.TrimEnd("He".ToCharArray()));//輸出Miracle: 等同於new char[]{'H', 'e'}
Console.WriteLine(testStr);//本身保持不變,輸出Miracle He

//使用Split來分割字符串
string strA = "aaa,bbb,ccc";
string[] arrayA = strA.Split(',');//等同於splitStr.Split(new char[]{','});
foreach (string elem in arrayA)
{
Console.WriteLine(elem);
}
//用多個字符分割字符串
string strB = "aaajbbbscccjdddseee";
string[] arrayB = strB.Split(new char[] { 'j', 's' });//等同於
strB.Split(new char[] { ',' });
foreach (string elem in arrayB)
{
Console.WriteLine(elem);
}
//用字符串分割字符串
string strC = "aaajsbbbjsccc";
string[] arrayC = Regex.Split(strC, "js", RegexOptions.IgnoreCase);
foreach (string elem in arrayC)
{
Console.WriteLine(elem);
}

Console.WriteLine(12345.ToString("n"));//輸出12,345.00
Console.WriteLine(12345.ToString("C"));//輸出¥12,345.00
Console.WriteLine(12345.ToString("e"));//輸出1.234500e+004
Console.WriteLine(12345.ToString("f4"));//輸出12345.0000
Console.WriteLine(12345.ToString("x"));//輸出3039
Console.WriteLine(12345.ToString("p"));//輸出1,234,500.00%

//將123456789轉化爲12-345-6789的3種方法
Console.WriteLine(int.Parse("123456789").ToString("##-###-####"));
Console.WriteLine("123456789".Insert(5, "-").Insert(2, "-"));
Regex regex = new Regex(@"^(\d{2})(\d{3})(\d{4})$");
Console.WriteLine(regex.Replace("123456789", "$1-$2-$3"));

//簡單輸出21個S(不用for循環)
string result = new string('S', 21);
Console.WriteLine(result);

//獲取隨機數
Random random = new Random();
Console.WriteLine(random.Next());//輸出非負隨機整數
Console.WriteLine(random.Next(10));//輸出小於10非負隨機整數
Console.WriteLine(random.Next() % 10);//輸出非負隨機整數
Console.WriteLine(random.Next(1, 20));//輸出指定範圍(1~20)非負隨機整數
Console.WriteLine(random.NextDouble());//輸出0.0~1.0非負隨機數

//int.TryParse,int.Parse,Convert.ToInt32之間的區別:
//性能上: int.TryParse > int.Parse > Convert.ToInt32.建議在.Net1.1使用int.Parse,在.Net2.0使用int.TryParse
int myInt = 0;
string testStr = "1234";
int.TryParse(testStr, out myInt);
Console.WriteLine(myInt);
Console.WriteLine(int.Parse(testStr));
Console.WriteLine(Convert.ToInt32(testStr));
testStr = null;
int.TryParse(testStr, out myInt);
Console.WriteLine(myInt);//輸出0
//Console.WriteLine(int.Parse(testStr));//拋異常
Console.WriteLine(Convert.ToInt32(testStr));//輸出0

//幾個常見的數學函數及應用
Console.WriteLine(Math.Ceiling(1.4));//輸出2
Console.WriteLine(Math.Floor(1.4));//輸出1
Console.WriteLine(Math.Round(1.4));//輸出1
int ys = 0;
int s = Math.DivRem(5, 3, out ys);
Console.WriteLine("5/3的商:" + s + ",餘數: " + ys);
Console.WriteLine(Math.BigMul(2, 4));//輸出兩個32位整數的完整乘積8
Console.WriteLine((double)5 / 3.0);//輸出兩個數的商(除數與被除數類型必須相同)

 

轉載於:https://www.cnblogs.com/hmiinyu/archive/2011/11/04/2236252.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章