Objective-C 中的 NSRange 有问题吗?

发布于 2024-10-10 20:03:25 字数 410 浏览 9 评论 0原文

我对 NSRange 有一点问题,或者可能只是我使用了错误的命令。

这就是我想做的。我有一个像这样的字符串:

NSString *mystring = @"/c1blue/c2green/c3yellow/"

如您所见,总是有一个带有值的命令,并且用“/”分隔。现在我想编写一个方法,为我提供一个命令的特定值,例如 c2 ,它会是绿色的。

首先我会得到 c2 的位置:

int beginIndex = [mystring rangeOfString:@"c2"].location;

现在我需要找到“/”的位置以及'beginIndex'的偏移量。

这就是我不知道如何做的地方。

非常感谢您的帮助。 谢谢

I have a little problem with NSRange, or maybe its just the wrong comman I use.

Here is what I want to do. I have a string like this :

NSString *mystring = @"/c1blue/c2green/c3yellow/"

As you can see there is always a command with a value and that seperated by "/". Now I want to write a mthod that gives me a specific value for a command, e.g. c2 which would be green.

First I would get the position of c2 :

int beginIndex = [mystring rangeOfString:@"c2"].location;

Now I need to find the position of the "/" with the offset of 'beginIndex'.

And thats where I do not know how.

Help is hihly appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独夜无伴 2024-10-17 20:03:25

首先使用 NSStringcomponentsSeparatedByString: 方法将字符串拆分为数组怎么样?

How about splitting the string into array first, using the componentsSeparatedByString: method of NSString?

何时共饮酒 2024-10-17 20:03:25

怎么样:

NSString *mystring = @"/c1blue/c2green/c3yellow/";
NSMutableDictionary *commands=[NSMutableDictionary dictionary];
for (NSString *component in [mystring componentsSeparatedByString:@"/"])
{
    // assuming your identifier is always 2 characters...
    if ([component length]>2) {
        [commands setObject:[component substringFromIndex:2] forKey:[component substringToIndex:2]];
    }
}
NSLog(@"commands %@", commands);
NSLog(@"command c2: %@", [commands objectForKey:@"c2"]);

结果:

2011-01-06 15:21:27.117 so[3741:a0f] commands {
    c1 = blue;
    c2 = green;
    c3 = yellow;
}
2011-01-06 15:25:26.488 so[3801:a0f] command c2: green

How about this:

NSString *mystring = @"/c1blue/c2green/c3yellow/";
NSMutableDictionary *commands=[NSMutableDictionary dictionary];
for (NSString *component in [mystring componentsSeparatedByString:@"/"])
{
    // assuming your identifier is always 2 characters...
    if ([component length]>2) {
        [commands setObject:[component substringFromIndex:2] forKey:[component substringToIndex:2]];
    }
}
NSLog(@"commands %@", commands);
NSLog(@"command c2: %@", [commands objectForKey:@"c2"]);

Result:

2011-01-06 15:21:27.117 so[3741:a0f] commands {
    c1 = blue;
    c2 = green;
    c3 = yellow;
}
2011-01-06 15:25:26.488 so[3801:a0f] command c2: green
泛滥成性 2024-10-17 20:03:25

另一种方法:

NSString *getCommand(NSString *string, NSString *identifier)
{
    for (NSString *component in [string componentsSeparatedByString:@"/"])
    {
        NSRange range=[component rangeOfString:identifier];
        if (range.location==0) {
            return [component substringFromIndex:range.length];
        }
    }
    return nil;
}

测试代码:

NSString *mystring = @"/c1blue/c2green/c3yellow/";
NSLog(@"%@", getCommand(mystring, @"c2"));
NSLog(@"%@", getCommand(mystring, @"c3"));
NSLog(@"%@", getCommand(mystring, @"c4"));  

结果:

2011-01-06 15:31:13.706 so[3949:a0f] green
2011-01-06 15:31:13.711 so[3949:a0f] yellow
2011-01-06 15:31:13.712 so[3949:a0f] (null)

Another approach:

NSString *getCommand(NSString *string, NSString *identifier)
{
    for (NSString *component in [string componentsSeparatedByString:@"/"])
    {
        NSRange range=[component rangeOfString:identifier];
        if (range.location==0) {
            return [component substringFromIndex:range.length];
        }
    }
    return nil;
}

Test code:

NSString *mystring = @"/c1blue/c2green/c3yellow/";
NSLog(@"%@", getCommand(mystring, @"c2"));
NSLog(@"%@", getCommand(mystring, @"c3"));
NSLog(@"%@", getCommand(mystring, @"c4"));  

Result:

2011-01-06 15:31:13.706 so[3949:a0f] green
2011-01-06 15:31:13.711 so[3949:a0f] yellow
2011-01-06 15:31:13.712 so[3949:a0f] (null)
小苏打饼 2024-10-17 20:03:25

看看 NSString 参考,我认为你需要的是:

- (NSArray *)componentsSeparatedByString:(NSString *)separator

Look at NSString reference and i think what you need is:

- (NSArray *)componentsSeparatedByString:(NSString *)separator

剩余の解释 2024-10-17 20:03:25

谢谢,

我实际上想出了这个,但它看起来比建议的更复杂:

- (NSString *) CommSubStr:(NSString *) commandString withPattern:(NSString *) command
{
    int beginIndex = [commandString rangeOfString:command].location;
    if (beginIndex == -1)
    {
       return @"";
    } else
    {
        NSRange subStringRange = NSMakeRange(beginIndex + [command length], [commandString length]-(beginIndex+[command length]));
        NSString *newString = [commandString substringWithRange:subStringRange];

        int endIndex = [newString rangeOfString:@"/"].location;
        NSRange finalSubStringRange = NSMakeRange(0, endIndex);

        return [newString substringWithRange:finalSubStringRange];
    }
}

Thanks,

I actually came up with this one, but it looks more complicated than the suggested ones :

- (NSString *) CommSubStr:(NSString *) commandString withPattern:(NSString *) command
{
    int beginIndex = [commandString rangeOfString:command].location;
    if (beginIndex == -1)
    {
       return @"";
    } else
    {
        NSRange subStringRange = NSMakeRange(beginIndex + [command length], [commandString length]-(beginIndex+[command length]));
        NSString *newString = [commandString substringWithRange:subStringRange];

        int endIndex = [newString rangeOfString:@"/"].location;
        NSRange finalSubStringRange = NSMakeRange(0, endIndex);

        return [newString substringWithRange:finalSubStringRange];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文