今天看了webcast上关于string类的一些用法,我根据视频写了一个综合的例子。例子如下:

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace CSharpCode
{
class StringTest
{
public static void Test()
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
Console.WriteLine(string.Format("{0}在 {1:yyyy年MM月dd日}到达扬州。", "小明", DateTime.Now));
double d = 123.67567;
Console.WriteLine(d.ToString("C3")); //结果为Y123.676; 3指定了小数的位数
string s = "1,2,3,4,5,6";
string[] s2 = s.Split(',');
foreach (string i in s2)
{
Console.WriteLine(i);
}
string join = string.Join("---", s2); //把string数组用指定符号连接起来
Console.WriteLine(join);
Console.WriteLine("------比较String相加和StringBuilder.Append()的执行效率-------");
DateTime before = DateTime.Now;
string a = "aaa";
for (int i = 0; i < 20000; i++)
{
a += i;
}
Console.WriteLine("String相加循环所花费的时间为{0}", DateTime.Now - before);
DateTime start = DateTime.Now;
StringBuilder sb = new StringBuilder("123");
for (int i = 0; i < 20000; i++)
{
sb.Append(i);
}
Console.WriteLine("StringBuilder.Append()循环所花费的时间为{0}", DateTime.Now - start);
string st = "123e";
int dd;
if (int.TryParse(st, out dd)) //判断能否转换成功
{
Console.WriteLine(dd);
}
else
{
Console.WriteLine("请输入正确的数字!");
}
//判断字符串是否为空
string em = "";
// if(em==null||em=="")
// 注意""相当于string.Empty
//还可以使用if(em.Length>0)的方式,如果大于0的话,那么就表示em不为null也不为空,
//特别注意这里是""和" "是不同的,后者是不为空的,它的长度是大于0的
if (string.IsNullOrEmpty(em))
{
Console.WriteLine("em是空字符串!");
}
string tr = "---bcd---";
string end = tr.Trim('-');
Console.WriteLine(string.Format("{0}:长度为{1}", end, end.Length));
string yz = "aBc";
string sz = "AbC";
Console.WriteLine(yz.ToUpper() == sz.ToUpper());//不要使用ToLower()方法,因为ToUpper()是经过优化的,效率比较高
Console.WriteLine(string.Compare(yz, sz, true) == 0);//忽略大小写比较大小 微软MVP推荐使用这种方法
Regex re = new Regex("[a-zA-Z]+", RegexOptions.None);
string[] lines = re.Split("12r4t");
foreach (string line in lines)
{
Console.WriteLine(line);
}
MatchCollection mc = re.Matches("t45yu");
foreach (Match ma in mc)
{
Console.WriteLine(ma);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace CSharpCode
{
class StringTest
{
public static void Test()
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
Console.WriteLine(string.Format("{0}在 {1:yyyy年MM月dd日}到达扬州。", "小明", DateTime.Now));
double d = 123.67567;
Console.WriteLine(d.ToString("C3")); //结果为Y123.676; 3指定了小数的位数
string s = "1,2,3,4,5,6";
string[] s2 = s.Split(',');
foreach (string i in s2)
{
Console.WriteLine(i);
}
string join = string.Join("---", s2); //把string数组用指定符号连接起来
Console.WriteLine(join);
Console.WriteLine("------比较String相加和StringBuilder.Append()的执行效率-------");
DateTime before = DateTime.Now;
string a = "aaa";
for (int i = 0; i < 20000; i++)
{
a += i;
}
Console.WriteLine("String相加循环所花费的时间为{0}", DateTime.Now - before);
DateTime start = DateTime.Now;
StringBuilder sb = new StringBuilder("123");
for (int i = 0; i < 20000; i++)
{
sb.Append(i);
}
Console.WriteLine("StringBuilder.Append()循环所花费的时间为{0}", DateTime.Now - start);
string st = "123e";
int dd;
if (int.TryParse(st, out dd)) //判断能否转换成功
{
Console.WriteLine(dd);
}
else
{
Console.WriteLine("请输入正确的数字!");
}
//判断字符串是否为空
string em = "";
// if(em==null||em=="")
// 注意""相当于string.Empty
//还可以使用if(em.Length>0)的方式,如果大于0的话,那么就表示em不为null也不为空,
//特别注意这里是""和" "是不同的,后者是不为空的,它的长度是大于0的
if (string.IsNullOrEmpty(em))
{
Console.WriteLine("em是空字符串!");
}
string tr = "---bcd---";
string end = tr.Trim('-');
Console.WriteLine(string.Format("{0}:长度为{1}", end, end.Length));
string yz = "aBc";
string sz = "AbC";
Console.WriteLine(yz.ToUpper() == sz.ToUpper());//不要使用ToLower()方法,因为ToUpper()是经过优化的,效率比较高
Console.WriteLine(string.Compare(yz, sz, true) == 0);//忽略大小写比较大小 微软MVP推荐使用这种方法
Regex re = new Regex("[a-zA-Z]+", RegexOptions.None);
string[] lines = re.Split("12r4t");
foreach (string line in lines)
{
Console.WriteLine(line);
}
MatchCollection mc = re.Matches("t45yu");
foreach (Match ma in mc)
{
Console.WriteLine(ma);
}
}
}
}
742

被折叠的 条评论
为什么被折叠?



