我目前正在使用Treeview
控件處理WinForms
應用程序。我添加了一個複製機制來複制當前選擇的Treenode
。 複製的Treenode
對象的名稱應該按照以下方式與原始Treenode
不同: 「aNode」, 「aNode_1」, 「aNode_2」。用附加增量重命名複製的對象(即NewObject_1)
感覺就像Regex
是要走的路。
這裏是我到目前爲止有:
string theNameToBe = "aName";
List<string> theAlreadyInUseNames // Filled beforehand
int i = 1;
do
{
// ToDo: Use Regex to get XXXXX_1 -> XXXXX_2 as a possible name instead of XXXXX_1_1
Regex aPossibleNewNameRegex = new Regex(String.Format("^{0}[_]\\d+$", theNameToBe),RegexOptions.IgnoreCase);
// This does not take a previously created XXXXX_1 into account and continues to attach "_1"s to the string
string thePossibleNewName = String.Format(theNameToBe + "_{0}", i);
if (!theAlreadyInUseNames.Any(s => s.Equals(thePossibleNewName, StringComparison.OrdinalIgnoreCase)))
{
theNextAvailableName = thePossibleNewName;
break;
}
i++;
} while (i < int.MaxValue);
這隻會變得像「陽極」隨後的名稱,「aNode_1」,「aNode_1_1」,......等等。
請問您可以幫我解決我在C#
中handlin正則表達式的缺乏專業知識嗎?
編輯澄清: 應確定下一個可用的空白點。當後面的某個節點/名稱被刪除時,會有一個空白點。因此,如果僅存在字符串「aName」,「aName_1」和「aName_3」,則「aName_2」是正確的發現。
您是重命名還是生成名稱?因爲生成和'$'{name} _ {i ++}「'一樣簡單。 – Sinatr
這是關於確定下一個可用點。就像刪除aName_2之後,下一個複製的節點應該插入到aName_1和aName_3之間,而不是簡單地添加另一個並將其命名爲aName_4。 好點,謝謝。我會試着在問題中澄清一下。 –
問題不清楚。 你有什麼(有例子)?和你想要得到什麼? – nAviD