IOS:NSString 从字符串中检索子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8912146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:23:00  来源:igfitidea点击:

IOS : NSString retrieving a substring from a string

iphoneiosstringnsstring

提问by Burf2000

Hey I am looking for a way to extract a string from another string. It could be any length and be in any part of the string so the usual methods don't work.

嘿,我正在寻找一种从另一个字符串中提取字符串的方法。它可以是任意长度并且可以位于字符串的任何部分,因此通常的方法不起作用。

For example http://bla.com/bla?id=%1234%&something=%888%

例如 http://bla.com/bla?id=%1234%&something=%888%

What I want to extract is from id=% to the next %.

我要提取的是从 id=% 到下一个 %。

Any idea's?

有任何想法吗?

回答by Nick Lockwood

Use the rangeOfString method:

使用 rangeOfString 方法:

NSRange range = [string rangeOfString:@"id=%"];
if (range.location != NSNotFound)
{
   //range.location is start of substring
   //range.length is length of substring
}

You can then chop up the string using the substringWithRange:, substringFromIndex:and substringToIndex:methods to get the bits you want. Here's a solution to your specific problem:

然后,您可以使用substringWithRange:,substringFromIndex:substringToIndex:方法切碎字符串以获得所需的位。这是您的具体问题的解决方案:

NSString *param = nil;
NSRange start = [string rangeOfString:@"id=%"];
if (start.location != NSNotFound)
{
    param = [string substringFromIndex:start.location + start.length];
    NSRange end = [param rangeOfString:@"%"];
    if (end.location != NSNotFound)
    {
        param = [param substringToIndex:end.location];
    }
}

//param now contains your value (or nil if not found)

Alternatively, here's a general solution for extracting query parameters from a URL, which may be more useful if you need to do this several times:

或者,这里有一个从 URL 中提取查询参数的通用解决方案,如果您需要多次执行此操作,这可能更有用:

- (NSDictionary *)URLQueryParameters:(NSURL *)URL
{
    NSString *queryString = [URL query];
    NSMutableDictionary *result = [NSMutableDictionary dictionary];
    NSArray *parameters = [queryString componentsSeparatedByString:@"&"];
    for (NSString *parameter in parameters)
    {
        NSArray *parts = [parameter componentsSeparatedByString:@"="];
        if ([parts count] > 1)
        {
            NSString *key = [parts[0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSString *value = [parts[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            result[key] = value;
        }
    }
    return result;
}

This doesn't strip the % characters from the values, but you can do that either with

这不会从值中去除 % 字符,但您可以使用

NSString *value = [[value substringToIndex:[value length] - 1] substringFromIndex:1];

Or with something like

或者像

NSString *value = [value stringByReplacingOccurencesOfString:@"%" withString:@""];


UPDATE:As of iOS 8+ theres a built-in class called NSURLComponentsthat can automatically parse query parameters for you (NSURLComponentsis available on iOS 7+, but the query parameter parsing feature isn't).

更新:从 iOS 8+ 开始,有一个名为的内置类NSURLComponents可以为您自动解析查询参数(NSURLComponents在 iOS 7+ 上可用,但查询参数解析功能不可用)。

回答by Suraj K Thomas

Try this

尝试这个

NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"];

NSString* day = [foo objectAtIndex: 0];

NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"];

NSString* day = [foo objectAtIndex: 0];