我有一个textBox1显示text = 01/02/2013,我有 字符串年,月,日。
如何设定 年= 2013, 月= 02, 天= 01 来自textbox1
答案 0 :(得分:4)
var text = "01/02/2013";
var parts = text.Split('/');
var day = parts[0];
var month = parts[1];
var year = parts[2];
答案 1 :(得分:3)
只是为了与众不同并添加一个不拆分字符串的解决方案,这里有一个将字符串转换为DateTime并从生成的DateTime对象中提取信息。
class Program
{
static void Main(string[] args)
{
string myString = "01/02/2013";
DateTime tempDate;
if (!DateTime.TryParse(myString, out tempDate))
Console.WriteLine("Invalid Date");
else
{
var month = tempDate.Month.ToString();
var year = tempDate.Year.ToString();
var day = tempDate.Day.ToString();
Console.WriteLine("The day is {0}, the month is {1}, the year is {2}", day, month, year);
}
Console.ReadLine();
}
}
答案 2 :(得分:2)
使用string.Split获取每个字符串
string s = "01/02/2013";
string[] words = s.Split('/');
foreach (string word in words)
{
Console.WriteLine(word);
}
答案 3 :(得分:0)
试试这个正则表达式
(?<month>\d{1,2})\/(?<day>\d{1,2})\/(?<year>\d{4})
I / P:
2/7/2014
O / P:
month 2
day 7
year 2014
<强>(OR)强>
尝试String.Split方法
string[] separators = {"-","/",":"};
string value = "01/02/2013";
string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (void word_loopVariable in words)
{
word = word_loopVariable;
Console.WriteLine(word);
}