2013-03-28 84 views
-1

SO Im在創建一些共享代碼時卡在我的項目中。我有一個包含一些字符串的列表。我想過濾這個,然後將過濾後的字符串分開。我已經在objective-c中完成了這個工作,但是我不知道如何在c#中完成這項工作。過濾和分割位置

的Objective-C會是這樣的:

NSPredicate *findStringWithStatusPlanned = [NSPredicate predicateWithFormat:@"SELF contains[c] 'TOMTOM101'"]; 
     NSArray *filteredArrayOnStatusPlanned = [trackTraceContentFiltered filteredArrayUsingPredicate:findStringWithStatusPlanned]; 
     NSLog(@"%@",filteredArrayOnStatusPlanned); 

     plannedStatusCompleet = [filteredArrayOnStatusPlanned lastObject]; 
      janssenRef = [plannedStatusCompleet substringWithRange:NSMakeRange(0, 8)]; 
      clientRef = [plannedStatusCompleet substringWithRange:NSMakeRange(9, 15)]; 
      dateReceived = [plannedStatusCompleet substringWithRange:NSMakeRange(25, 10)]; 
      tomTomCode = [plannedStatusCompleet substringWithRange:NSMakeRange(36, 9)]; 
      orderText = [plannedStatusCompleet substringWithRange:NSMakeRange(47, 59)]; 
      podFileName = [plannedStatusCompleet substringWithRange:NSMakeRange(113, 19)]; 
      Plannedidentifier = [plannedStatusCompleet substringWithRange:NSMakeRange(62, 1)]; 
      NSLog(@"Planid:%@",Plannedidentifier); 

     NSArray *cutContentPlannedStatus = [NSArray arrayWithObjects:janssenRef,clientRef,dateReceived,tomTomCode,orderText,podFileName,Plannedidentifier,nil]; 
     [[NSUserDefaults standardUserDefaults] setObject:cutContentPlannedStatus forKey:@"plannedData"]; 

我tryed分裂這樣的:

foreach (string s in TrackTraceDataByLine) { 
       string[] splitted = s.Split (new char[] { ' ' }); 
       if (s.Length >= referenceNumber.Length) { 
        if (referenceNumber == splitted [1]) { 
         dataFilteredOnRef.Add (s); 
        } 
       } 
      } 

我可以很快的意識到,我有能力做它而不是分裂在空間上。這是因爲像參考號碼這樣的部分可以包含空格。

+0

@Axarydax看到編輯:) –

回答

2

您的意思是?

var janssenRef = sourceString.Substring(0, 8); 
var clientRef = sourceString.Substring(9, 15); 
var dateReceived = sourceString.Substring(25, 10); 
... 
var array = new string[] { ... }; 

要在LINQ的風格做到這一點,你可以使用交錯數組:

var pos = new int[][] { 
    new int[] { 0, 8 }, 
    new int[] { 9, 15 }, 
    new int[] { 25, 10 }, ... }; 
var array = pos.Select(p => sourceString.Substring(p[0], p[1])) 
       .ToArray(); 

或者多維數組:

var pos = new int[,] { { 0, 8 }, { 9, 15 }, { 25, 10 }, ... }; 
var array = Enumerable.Range(0, pos.GetLength(0)) 
         .Select(i => inputString.Substring(pos[i,0], pos[i,1])) 
         .ToArray(); 
+0

要試試這個明天但它看起來不錯!:)。 –

+0

'{0,3}'應該是'{0,8}',但是很好的解決方案! – qujck

+0

錯誤CS0623:數組初始值設定項只能用於變量或字段初始值設定項中。嘗試使用新的表達式(CS0623)(TrackandTrace.IOS) –