0

特定のパターンの文字列があります。パターンを検索し、そのパターン内の文字列を置き換える必要があります。例:

NSString *string = @"{Hello} ({World}) ({How}) ({Are}) ({You})";
NSString *result = nil;

// Determine "{" location
NSRange startRange = [string rangeOfString:@"{" options:NSCaseInsensitiveSearch];
if (startRange.location != NSNotFound)
{
    // Determine "}" location according to "{" location
    NSRange endRange;

    endRange.location = startRange.length + startRange.location;
    endRange.length   = [string length] - endRange.location;
    endRange = [string rangeOfString:@"}" options:NSCaseInsensitiveSearch range:endRange];

    if (endRange.location != NSNotFound)
    {
        // bracets found: retrieve string between them
        startRange.location += startRange.length;
        startRange.length = endRange.location - startRange.location;

        result = [string substringWithRange:startRange];

    }
}

ここで、「{ }」の間にある最初の部分文字列を抽出できます。つまり、「Hello」ですが、チェックを続行する必要があり、他の文字列を抽出する必要もあります。

4

3 に答える 3

2

これを試してください:

NSString *string = @"{Hello} ({World}) ({How}) ({Are}) ({You})";
    //NSString *result = nil;

    // Determine "{" location

    NSArray *array=[string componentsSeparatedByString:@"{"];
    for(NSString *str in array){
        NSString *newString=[[str componentsSeparatedByString:@"}"] objectAtIndex:0];
    NSLog(@"%@",newString);
    }
于 2011-12-12T07:30:53.873 に答える
0

私はたまたまこのコードをうそをついています。私はそれがあなたが望むことを正確に行うと思います。のカテゴリとして実装しましたNSString。次のように使用します。

NSString *template = @"{Hello} ({World}) ({How}) etc etc";
NSDictionary *vars = [NSDictionary dictionaryWithObjectsAndKeys:
    @"Bonjour", @"Hello",
    @"Planet Earth", @"World",
    @"Como", @"How",
    // etc.
    nil];

NSString *expandedString = [template stringByExpandingTemplateWithVariables:vars];
// expandedString is @"Bonjour (Planet Earth) (Como) etc etc"

これがコードです。

ファイルNSString+TemplateExpansion.h

#import <Foundation/Foundation.h>

@interface NSString (TemplateExpansion)

- (NSString *)stringByExpandingTemplateWithVariables:(NSDictionary *)dictionary;

@end

ファイルNSString+TemplateExpansion.m

#import "NSString+TemplateExpansion.h"

@implementation NSString (TemplateExpansion)

- (NSString *)stringByExpandingTemplateWithVariables:(NSDictionary *)dictionary
{
    NSUInteger myLength = self.length;
    NSMutableString *result = [NSMutableString stringWithCapacity:myLength];

    NSRange remainingRange = NSMakeRange(0, myLength);
    while (remainingRange.length > 0) {
        NSRange leftBraceRange = [self rangeOfString:@"{" options:0 range:remainingRange];
        if (leftBraceRange.location == NSNotFound)
            break;

        NSRange afterLeftBraceRange = NSMakeRange(NSMaxRange(leftBraceRange), myLength - NSMaxRange(leftBraceRange));
        NSRange rightBraceRange = [self rangeOfString:@"}" options:0 range:afterLeftBraceRange];
        if (rightBraceRange.location == NSNotFound)
            break;

        NSRange beforeLeftBraceRange = NSMakeRange(remainingRange.location, leftBraceRange.location - remainingRange.location);
        [result appendString:[self substringWithRange:beforeLeftBraceRange]];
        remainingRange = NSMakeRange(NSMaxRange(rightBraceRange), myLength - NSMaxRange(rightBraceRange));

        NSRange keyRange = NSMakeRange(NSMaxRange(leftBraceRange), rightBraceRange.location - NSMaxRange(leftBraceRange));
        NSString *key = [self substringWithRange:keyRange];
        NSString *value = [dictionary objectForKey:key];
        if (value)
            [result appendString:value];
    }

    [result appendString:[self substringWithRange:remainingRange]];
    return result;
}

@end
于 2011-12-12T07:58:54.210 に答える
0

これを試して :

NSString *string = @"{Hello} ({World}) ({How}) ({Are}) ({You})";
    NSMutableString *result = [[NSMutableString alloc] init];

    NSArray *tempArray = [[string componentsSeparatedByString:@" "] mutableCopy];

    for (int i=0; i < [tempArray count]; i++)
    {
        NSString *tempStr = [tempArray objectAtIndex:i];
        NSRange startRange = [tempStr rangeOfString:@"{" options:NSCaseInsensitiveSearch];
        if (startRange.location != NSNotFound)
        {
            // Determine "}" location according to "{" location
            NSRange endRange;

            endRange.location = startRange.length + startRange.location;
            endRange.length   = [tempStr length] - endRange.location;
            endRange = [tempStr rangeOfString:@"}" options:NSCaseInsensitiveSearch range:endRange];

            if (endRange.location != NSNotFound)
            {
                // bracets found: retrieve string between them
                startRange.location += startRange.length;
                startRange.length = endRange.location - startRange.location;

                //result = [tempStr substringWithRange:startRange];
                [result appendString:[NSString stringWithFormat:@"%@ ",[tempStr substringWithRange:startRange]]];
                NSLog(@"%@ ",result);

            }
        }
    }

との世話をreleaseするtempArrayresult

于 2011-12-12T07:21:58.457 に答える