2014-01-08 37 views
0

我有一個TimeSpan,其格式爲@"hh\:mm\:ss\.fff",並且希望使用xceed(https://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox&referringTitle=Home)中的MaskedTextBox來幫助用戶輸入有效的時間範圍。將TimeSpan格式字符串轉換爲InputMask

現在我轉換FormatString,物業到輸入掩碼像這樣

public string InputMask 
{ 
    get 
    { 
    string mask = FormatString.Replace('h', '0'); 
    mask = mask.Replace('m', '0'); 
    mask = mask.Replace('s', '0'); 
    mask = mask.Replace('f', '0'); 
    mask = mask.Replace('d', '0'); 
    return mask; 
    } 
} 

這個解決方案看起來醜陋,而不是維護,如果formatString的獲得,我還不知道另一種格式。有沒有更優雅的解決方案(例如用正則表達式替換),用0取代任何字母?

+0

[這](http://stackoverflow.com/questions/9975640/check-if-char-isletter)?但我會用'for(int i = 0; i Sinatr

+0

這不起作用,因爲'mask [i]'沒有setter,而字符串是不可變的。 – Herm

+0

然後我會來stackoverflow和張貼在這裏我的問題= D – Sinatr

回答

2

這應該做的伎倆:

//using System.Text.RegularExpressions; 

string input = @"hh\:mm\:ss\.fff"; //i suppose it's FormatString in your case, don't know the MaskedTextBox 
string output = Regex.Replace(input, "[a-zA-Z]","0");