2011-07-22 6 views
1

我有一個文件,我上傳到一個RichTextBox,然後將其格式化後顯示給列表框。列表框看起來像這樣(即我刪除所以它是用較短的工作減去一些線)C# - 通過它的'字符串排序列表框&刪除字符串的一部分

C44  EXCLUDES 237.910 193.469 0 0603_5 
C45  EXCLUDES 244.102 193.387 0 0603_5 
R47  EXCLUDES 226.935 179.519 90 0402_2 
C18  CAP-00129G 230.960 190.619 0 0402_4 
C17  CAP-00129G 250.085 198.569 180 0402_3 
Q7  IC-00268G 258.460 205.594 0 SOT236 
C25  CAP-00130G 255.635 189.669 90 0402_3 
C56  EXCLUDES 229.430 189.374 0 0402_4 
R42  EXCLUDES 241.010 192.194 90 TANT3216 
R21  CAP-00129G -123.370 -112.114 270 0402_3 
R10  EXCLUDES 246.560 203.894 0 0402_9 
...  .......... ....... ....... ... ........ 

不管怎麼說,我想排序的字符串結尾的列表框...所以第6個列(0603_5,0603_5,0402_2,0402_4,0402_3,TANT3216,0402_9 ....)中的值。通過這樣做,這將導致新的ListBox以此順序訂購(雖然不是所有的這些在文件示例所示)

RES, 0402, 0201, 0603, 0805, 1206, 1306, 1608, 3216, 2551, 1913, 1313, 2513, 5125, 2525, 5619, 3813, 1508, 6431, 2512, 1505, 2208, 1005, 1010, 2010, 0505, 0705, 1020, 1812, 2225, 5764, 4532, 1210, 0816, 0363, SOT.

(注意TANT3216SOT236因爲之前它會關閉數不TANT在上面的訂貨清單)

另外,如果有多個結局是相似(見* 0402_3 * AB ove及以下),那麼列表中的項目將按2nd列進行排序。因此,即使以R21開始的行在以C25開始的行之後,並且它們都以* 0402_3 *結尾,R21將被放置在C25之上,因爲它在第6列之後檢查第2列(這是從最小到最大排序的)。

SO,新的文件應該是這樣的:

R47  EXCLUDES 226.935 179.519 90 0402_2 
C17  CAP-00129G 250.085 198.569 180 0402_3 
R21  CAP-00129G -123.370 -112.114 270 0402_3 
C25  CAP-00130G 255.635 189.669 90 0402_3 
C18  CAP-00129G 230.960 190.619 0 0402_4 
C56  EXCLUDES 229.430 189.374 0 0402_4 
R10  EXCLUDES 246.560 203.894 0 0402_9 
C44  EXCLUDES 237.910 193.469 0 0603_5 
C45  EXCLUDES 244.102 193.387 0 0603_5 
R42  EXCLUDES 241.010 192.194 90 TANT3216 
Q7  IC-00268G 258.460 205.594 0 SOT236 
...  .......... ....... ....... ... ........ 

一旦列表框進行排序我有一個按鈕(即不能正常工作)來替換每個字符串的結束值在列表框中輸入" "於是,就點擊一個按鈕,我想「REMOVE ENDINGS」並重新上傳到同一個ListBox。 因此同樣(更新)列表框應該是這樣的:

R47  EXCLUDES 226.935 179.519 90 
C17  CAP-00129G 250.085 198.569 180 
R21  CAP-00129G -123.370 -112.114 270 
C25  CAP-00130G 255.635 189.669 90 
C18  CAP-00129G 230.960 190.619 0 
C56  EXCLUDES 229.430 189.374 0 
R10  EXCLUDES 246.560 203.894 0 
C44  EXCLUDES 237.910 193.469 0 
C45  EXCLUDES 244.102 193.387 0 
R42  EXCLUDES 241.010 192.194 90 
Q7  IC-00268G 258.460 205.594 0 
...  .......... ....... ....... ... 

不工作(CODE):

private void removePackageButton_Click(object sender, EventArgs e) 
{ 
    System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items; 
    foreach (string str in contents) 
    { 
     List<string> list = str.Split(' ').ToList(); 
     if (list.Count == 6) 
     { 
      string remove = list[5]; 
      list.Remove(remove); 

      placementOneListBox.Items.Equals(list.ToArray()); 
     } 
    } 
} 

問題:

  • 我不知道如何去做這件事,我正在尋找任何可能的幫助。
  • 有沒有另外一種方法可以解決這個問題呢?
+4

你應該想想這個數據存儲到一些數據結構,而不是試圖操縱的字符串表示數據一直在。一旦你定義了一個數據結構,你就可以很容易地完成各種排序。 – Odnxe

+0

'placementOneListBox.Items.Equals(..)'不是你想要的 - 你可以使用'.Items.Add(..)'/'.Items.Remove(..)'將項目添加或刪除到列表框中。' – BrokenGlass

+0

@Odnxe:我對C#非常陌生,一直在努力學習它..哈哈。我怎麼能這樣做呢? – theNoobGuy

回答

1

如果你只是想做簡單的字符串操作,將ListBox的Item集合複製到一個臨時字符串數組中,對數組做一些工作,並使用Items的Clear()和AddRange()方法將其複製回來。我喜歡使用lambda表達式來進行這種操作。

排序

// Create a regular expression to identify the last column after the whitespace 
Regex pattern = new Regex("\\s+\\S+$"); 

// Dimension an array large enough 
string[] items = new string[lbContent.Items.Count]; 

// Copy the items 
lbContent.Items.CopyTo(items, 0); 

// Use an orderby to sort and convert resulting IEnumerable<> back to array 
items = (from i in items 
     let m = pattern.Match(i).Value 
     orderby m.Trim() 
     select i).ToArray(); 

// Place it back in the collection 
lbContent.Items.Clear(); 
lbContent.Items.AddRange(items); 

修剪

Regex pattern = new Regex("\\s+\\S+$"); 
string[] items = new string[lbContent.Items.Count]; 
lbContent.Items.CopyTo(items, 0); 

// Replace the last column with empty strings 
items = (from i in items 
     let m = pattern.Match(i).Value 
     select i.Replace(m, string.Empty)).ToArray(); 

lbContent.Items.Clear(); 
lbContent.Items.AddRange(items); 

或者,如果你感覺大膽...

兩者同時進行

Regex pattern = new Regex("\\s+\\S+$"); 
string[] items = new string[lbContent.Items.Count]; 
lbContent.Items.CopyTo(items, 0); 

items = (from i in items 
     let m = pattern.Match(i).Value 
     orderby m.Trim() 
     select i.Replace(m, string.Empty)).ToArray(); 

lbContent.Items.Clear(); 
lbContent.Items.AddRange(items); 

== ==編輯

兩者同時進行,修剪字符串

Regex pattern = new Regex("\\s+\\S+$"); 
string[] items = new string[lbContent.Items.Count]; 
lbContent.Items.CopyTo(items, 0); 

items = (from i in items 
     let t = i.Trim(), 
     m = pattern.Match(t).Value 
     orderby m 
     select t.Replace(m, string.Empty)).ToArray(); 

lbContent.Items.Clear(); 
lbContent.Items.AddRange(items); 
+1

I嘗試過大膽,並得到錯誤**字符串不能是零長度。參數名稱:oldValue ** ..我試圖將'string.Empty'切換爲'「」',它也表示同樣的事情。 : -/ – theNoobGuy

+0

這意味着你的集合中有空字符串的可能性很大。您不能對空字符串執行替換操作。沒有東西可以替代。 – lsuarez

+0

我想有一個空字符串。還有另外一種方法可以解決這個問題,或者忽略空字符串嗎? – theNoobGuy

相關問題