我在C#中有一些字符串,如下所示:
interesting, fun, May 08, 2012
this is very interesting text, June 19, 2011
我想得到2个字符串,一个带有日期,另一个带有所有字符串
所以输出应该是这样的:
string1=interesting, fun
string2=May 08, 2012
string1=this is very interesting text
string2=June 19, 2011
欣赏任何提示。
答案 0 :(得分:1)
好的,我知道有比这更好的方法(比如Regex),但这里有一个快速而又脏的方式:
string str1 = "blah, blah, May 08, 2012";
string str2 = "blah blah blah, June 19, 2011";
int splitter = str1.Substring(0, str1.LastIndexOf(',')).LastIndexOf(',');
string newStr1 = str1.Substring(0, splitter);
string newStr2 = str1.Substring(splitter + 2, str1.Length - (splitter + 2));
Console.WriteLine(newStr1);
Console.WriteLine(newStr2);
Console.ReadKey();
答案 1 :(得分:0)
bool foundDateComma = false;
string beginning, date;
for (int i = s.Length - 1; i >= 0; i--)
if (s[i] == ',')
if (!foundDateComma)
foundDateComma = true;
else
{
beginning = s.Substring(0, i);
date = s.Substring(i + 1);
break;
}
答案 2 :(得分:0)
正则表达式可以帮助您:
Match match = Regex.Match(yourstring, @"[January|February|March|April|May|June|July|August|September|October|November|December]\s\d+,\s\d+");
string date = match.Groups[1].Value;
string blah = yourstring.Substring(0, yourstring.IndexOf(date) -1);
答案 3 :(得分:0)