C#中怎样删除字符串两端的字符?

来源:百度知道 编辑:UC知道 时间:2024/06/17 12:58:26
如:
string test="2008年5月16日";
我只想获取这个字符串中的"5","16"要怎样做??
如果不是日期呢?就随意一个字符串.

string time,month,day;
string[] item;

time = "2008年5月16日";
item = time.Split('年');
time = item[1];
item = time.Split('月');
month = item[0];
time = item[1];
item = time.Split('日');
day = item[0];

Console.WriteLine("{0}", month);
Console.WriteLine("{0}", day);
Console.ReadKey();
上述程序最后得到的month,day就分别是5和16
任意字符串类似解决

先转换成日期,再提取相应的资料,都是有相应的函数!!

split("年")[1].tostring;
split("月")[0].tostring;

不停的分割

string test = "2008年5月16日";
char[] c = new char[25];
c = test.ToCharArray();

Response.Write(c[5] + "<br>" + c[7] + c[8]);
运行通过。

string str = "abcdefg";
string str1 = str.Substring(1,2);
//str1 = "bc";

通常用的方法是字符串截取
另外正则表达式也可以.