iOS获取2个字符串之间的字符串

时间:2014-12-19 13:29:36

标签: ios objective-c swift string substring

如何在两个已知字符串之间获取字符串?例如:

<id> 100 </id>

我想从这个字符串中获得100。

我试过这段代码,但它不起作用。 NSLog是:

<id>100

代码:

NSString *serverOutput = @"<id>100</id>";
NSRange start = [serverOutput rangeOfString:@"<id>"];
NSRange end = [serverOutput rangeOfString:@"</id>"];
NSLog(@"%@",[serverOutput substringWithRange:NSMakeRange(start.location, end.location)]);

6 个答案:

答案 0 :(得分:8)

你几乎就在那里,但范围的位置是不是结束的开始。所以你必须添加范围的长度。

由于您移动了字符串的开头,因此您需要使用偏移量缩短长度:

NSString *serverOutput = @"<id>100</id>";
NSRange startRange = [serverOutput rangeOfString:@"<id>"];
NSRange endRange = [serverOutput rangeOfString:@"</id>"];

NSInteger start = NSMaxRange (startRange);
NSInteger length = endRange.location - startRange.length;

NSLog(@"%@", [serverOutput substringWithRange:NSMakeRange(start, length)]);

答案 1 :(得分:2)

 NSString *serverOutput = @"<id>100</id>";
serverOutput = [serverOutput stringByReplacingOccurrencesOfString:@"<id>" withString:@""];
serverOutput = [serverOutput stringByReplacingOccurrencesOfString:@"</id>" withString:@""];

NSLog(@"%@",serverOutput);

答案 2 :(得分:2)

快速中:

extension String {

    func slice(from: String, to: String) -> String? {

        return (range(of: from)?.upperBound).flatMap { substringFrom in
            (range(of: to, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in
                String(self[substringFrom..<substringTo])
            }
        }
    }
}

输入

let serverOutput = "<id>100</id>"
let resultString = serverOutput.slice(from: "<id>", to: "</id>")
print(resultString)

输出

  

100

答案 3 :(得分:1)

这是NSRange定义的方式:

typedef struct _NSRange {
    NSUInteger location;
    NSUInteger length;
} NSRange;

所以,如果你想获得100,你应该像这样子串:

NSRange substringRange = NSMakeRange(start.location + start.length, end.location - start.length);
NSLog(@"%@", [serverOutput substringWithRange:substringRange]);

答案 4 :(得分:1)

我认为你应该使用正则表达式。

NSString *serverOutput = @"<id>100</id>";
NSString *pattern = @"<id>([0-9]+)</id>";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:serverOutput
                                                options:NSMatchingReportProgress
                                                  range:NSMakeRange(0, serverOutput.length)];
if (!error && match && match.numberOfRanges >= 2) {
    NSRange range = [match rangeAtIndex:1];
    NSLog(@"%@", [serverOutput substringWithRange:range]);
}

答案 5 :(得分:0)

你想从第一个字符串的末尾开始,所以你只需要改变:

NSLog(@"%@",[serverOutput substringWithRange:NSMakeRange(start.location, end.location)]);

要:

NSLog(@"%@",[serverOutput substringWithRange:NSMakeRange(NSMaxRange(start), end.location - start.length)]);

请注意,我还减去了起始字符串的长度,因为我们只是偏移了那么多。