0

私は次のようなテキストを取得しています:- 1. e4 e5 2. Nf3 Nc6 3. Bb5 {This opening is called the Ruy Lopez.} 3... a6 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5

{内のコメント}を削除する必要がありますが、その前に、3やBb5のように、移動のインデックス番号と一緒にコピーする必要があります。

私が使用している文字列を削除するために

- (NSString *)stringFilter:(NSString *)targetString {

    NSScanner *theScanner;
    NSString *text = nil;

    theScanner = [NSScanner scannerWithString: targetString];

    while ([theScanner isAtEnd] == NO) {

        [theScanner scanUpToString:@"{" intoString:NULL] ; 

        [theScanner scanUpToString:@"}" intoString:&text] ;

        targetString = [targetString stringByReplacingOccurrencesOfString:
                      [NSString stringWithFormat:@"%@}", text]
                                                           withString:@""];

    } 

    return targetString;

}

その文字列をコピーして辞書や配列に保存する方法を探しています。

4

1 に答える 1

0

次のカテゴリメソッドを使用して、「(」と「)」の間のコンテンツを取得します。あなたはそれを簡単に適応させることができます...

編集:完全性のための完全なカテゴリ

//  NSString+Parenthesis.h
#import <Foundation/Foundation.h>

@interface NSString (Parenthesis)

-(NSString*)contentsInParenthesis;

@end

//  NSString+Parenthesis.m
#import "NSString+Parenthesis.h"

@implementation NSString (Parenthesis)

-(NSString*)contentsInParenthesis {
    NSString *subString = nil;
    NSRange range1 = [self rangeOfString:@"("];
    NSRange range2 = [self rangeOfString:@")"];

    if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location)) {
        NSRange range3;
        range3.location = range1.location+1;
        range3.length = (range2.location - range1.location)-1;

        subString = [self substringWithRange:range3];
    }

    return subString;
}

@end
于 2012-05-31T07:46:18.100 に答える