1

私はこのように見える多くのNSStringを持っています:

@"this is the content. The content of this string may vary, as well as the length, and my include any characters, with numbers Y = 295.000000 X = 207.500000"

パーツY=295.000000 X = 207.500000は、変更される可能性のあるX番号とY番号を除いて、常に同じままです。

私はそれらの数字を取得し、次のようなことをするために何らかの方法で必要です:

coordsFinal.x = 295.000000;
coordsFinal.y = 207.500000;

次の形式になります。

coordsFinal.x = [NSString stringWithFormat: @"%@", trimmed string];

何か案は??

4

2 に答える 2

5

正規表現を使用して数値を抽出できます。

NSString *string = ...; // Your string
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Y = (\\d+.\\d+) X = (\\d+.\\d+)" options:0 error:NULL];
NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
if (match) {
    NSRange yRange = [match rangeAtIndex:1];
    NSString *yString = [string substringWithRange:yRange];
    NSRange xRange = [match rangeAtIndex:2];
    NSString *xString = [string substringWithRange:xRange];

    NSLog(@"X = %@, Y = %@", xString, yString);
}

出力:

X = 207.500000, Y = 295.000000
于 2013-02-07T18:59:52.670 に答える
0

文字列をトリミングするために呼び出すには、次のメソッドが必要です。

 [yourstring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
于 2013-02-07T19:17:27.410 に答える