nsstring替换范围内的字符串

问题描述 投票:0回答:3

我有一个具有特定模式的字符串。我需要搜索模式并替换该模式中的字符串。例如:

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”,但我还需要继续检查并想要提取其他字符串。

iphone nsstring nsrange
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);
    }

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);

            }
        }
    }

照顾releasetempArrayresult


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"

这是代码。

File NSString+TemplateExpansion.h

#import <Foundation/Foundation.h>

@interface NSString (TemplateExpansion)

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

@end

File 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
© www.soinside.com 2019 - 2024. All rights reserved.