-1

所以我在创建一些共享代码时坚持我的项目。我有一个包含一些字符串的列表。我想过滤它,然后在位置上拆分过滤后的字符串。我已经在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"];

我试图这样拆分:

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

我可以很快意识到我必须按位置而不是按空间拆分。这是因为某些部分(如参考编号)可能包含空格。

4

1 回答 1

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();
于 2013-03-28T15:03:17.463 回答