我有一個字符串01-Jan-2014 00:00:00
,我打算將年份縮短爲2個字符。Regex.Replace和String.Replace不起作用
我的代碼:
DateTime dtParsedDate = new DateTime();
string strInput = "01-Jan-2014 00:00:00";
Regex regDate = new Regex(@"\d{2}-\w{3}-\d{4}");
// parse into datetime object
dtParsedDate = DateTime.ParseExact(regDate.Match(strInput).Value, "dd-MMM-yyyy", CultureInfo.InvariantCulture);
// replace the string with new format
regDate.Replace(arrData[iCol], dtParsedDate.ToString("dd-MMM-yy"));
我驗證過的字符串使用正則表達式正確匹配。
「01-Jan-2014」未被替換爲「01-Jan-14」。我的代碼有什麼問題?
[閱讀文檔(http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(V = vs.110)的.aspx)。 'Regex.Replace'返回一個新的字符串,它不會改變你傳入的字符串(因爲字符串是不可變的)。 –