2012-01-10 57 views
0

我想讓無錯誤的用戶輸入。當有一個拆分符號用戶可能會輸入它並創建一個錯誤,我必須在執行創建更多行代碼之前修復這個錯誤。有沒有使用例如$%$而不是$字符來分割字符串的方法?如何分割使用多個字符?

這是我使用一個字符做分割:

if (!lastUsed.EmptyFile()) 
{ 
    string[] allSettings = lastUsed.Text.Split('$'); 
    int settingCount = 0; 

    foreach (string setting in allSettings) 
    { 
     settingCount++; 

     if (settingCount == 1) 
     { 
      txtText.Text = setting; 
     } 
     else if (settingCount == 2) 
     { 
      if (setting == "0") tbType.SelectedTab = tbInterval; 
      else tbType.SelectedTab = tbRange; 
     } 
     else if (settingCount == 3) 
     { 
      nudInterval.Value = decimal.Parse(setting); 
     } 
     else if (settingCount == 4) 
     { 
      nudMin.Value = decimal.Parse(setting); 
     } 
     else if (settingCount == 5) 
     { 
      nudMax.Value = decimal.Parse(setting); 
     } 
    } 
} 
+4

您是否檢查過[文檔](http://msdn.microsoft.com/zh-cn/library/b873y76a.aspx)?它接受一組字符。聽起來像這就是你想要的:只需指定多個字符來分割。 – 2012-01-10 05:10:17

+0

這已經是你最後一部分http://stackoverflow.com/questions/8798603/input-string-was-not-in-a-correct-format-when-setting-a-numericupdown-value – V4Vendetta 2012-01-10 05:20:14

回答

3

您可以使用一個字符串作爲分隔符。

string[] delim = new string[] {"$%$"}; 

string[] allSettings = lastUsed.Text.Split(delim, StringSplitOptions.None); 
+0

簡單而正確點! +1! – HelpNeeder 2012-01-10 05:16:34

相關問題