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);//输出两个数的商(除数与被除数类型必须相同)

 

posted @ 2011-11-04 15:01  Miracle He  阅读(355)  评论(0编辑  收藏  举报