2016-02-18 22 views
0

我目前正在使用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」是正確的發現。

+0

您是重命名還是生成名稱?因爲生成和'$'{name} _ {i ++}「'一樣簡單。 – Sinatr

+0

這是關於確定下一個可用點。就像刪除aName_2之後,下一個複製的節點應該插入到aName_1和aName_3之間,而不是簡單地添加另一個並將其命名爲aName_4。 好點,謝謝。我會試着在問題中澄清一下。 –

+0

問題不清楚。 你有什麼(有例子)?和你想要得到什麼? – nAviD

回答

0

我可能已經發現,滿足要求的解決方案。這是第一份草案,可能會在一輪單元測試和現實生活場景後更新。 而且這個版本總是有可能成爲複雜的工作方式,而Sinatrs纖薄的方法可能就足夠了。

static void Main(string[] args) 
    { 
     List<string> theAlreadyInUseNames = new List<string>(); 

     theAlreadyInUseNames.Add("aName"); 
     theAlreadyInUseNames.Add("aName_1"); 
     theAlreadyInUseNames.Add("aName_2"); 
     theAlreadyInUseNames.Add("aName_3"); 
     theAlreadyInUseNames.Add("aName_4"); 
     theAlreadyInUseNames.Add("aName_5"); 
     theAlreadyInUseNames.Add("aName_7"); 
     theAlreadyInUseNames.Add("aName_2_1"); 
     theAlreadyInUseNames.Add("aName_2_3"); 

     string theNameToBe0 = "aName"; 
     string theNameToBe1 = "aName_2"; 
     string theNameToBe2 = "aName_2_1"; 

     List<string> namesToBe = new List<string>(); 
     namesToBe.Add(theNameToBe0); 
     namesToBe.Add(theNameToBe1); 
     namesToBe.Add(theNameToBe2); 

     string[] splits; 
     char[] charSeparators = new char[] { '_' }; 
     string theNewNameToBe1 = string.Empty; 
     string theNextAvailableName = String.Empty; 

     foreach (var item in namesToBe) 
     { 
      splits = item.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); 


      if (splits.Length > 1) 
      { 
       int theNumber = Convert.ToInt32(splits.Last()); 

       theNewNameToBe1 = splits.ElementAt(0); 

       for (int i = 1; i < splits.Length - 1; i++) 
       { 
        theNewNameToBe1 = theNewNameToBe1 + "_" + splits.ElementAt(i); 
       } 

       int counter = 1; 
       string theActualNewName = string.Empty; 
       do 
       { 

        theActualNewName = theNewNameToBe1 + "_" + (theNumber + counter); 

        if (!theAlreadyInUseNames.Any(s => s.Equals(theActualNewName, StringComparison.OrdinalIgnoreCase))) 
        { 
         theNextAvailableName = theActualNewName; 
         break; 
        } 

        counter++; 
       } while (counter < int.MaxValue); 
      } 
      else if (splits.Length == 1) 
      { 
        int counter = 1; 
        string theActualNewName = string.Empty; 

        do 
        { 
         theActualNewName = theNameToBe + "_" + (counter); 

         if (!theAlreadyInUseNames.Any(s => s.Equals(theActualNewName, StringComparison.OrdinalIgnoreCase))) 
         { 
          theNextAvailableName = theActualNewName; 
          break; 
         } 

         counter++; 
        } while (counter < int.MaxValue); 
      } 
      else 
      { 
       throw new ArgumentException(); 
      } 
     } 
    } 
0

可能是那樣簡單

var selected = "node_3"; 
var names = new[] { "node_1", "node_3" }; 
var prefix = selected.Substring(0, selected.IndexOf('_') + 1); 
var number = 1; 
string name; // result 
do 
    name = prefix + number++; 
while (names.Contains(name));