如何获得大括号之间的子串?

时间:2013-02-19 13:50:45

标签: ios objective-c nsstring substring

我有一个字符串。

NSString *myString = @"{53} balloons";

如何获取子串53

10 个答案:

答案 0 :(得分:18)

NSString *myString = @"{53} balloons";
NSRange start = [myString rangeOfString:@"{"];
NSRange end = [myString rangeOfString:@"}"];
if (start.location != NSNotFound && end.location != NSNotFound && end.location > start.location) {
    NSString *betweenBraces = [myString substringWithRange:NSMakeRange(start.location+1, end.location-(start.location+1))];
}

编辑:增加了范围检查,thx到Keab42 - 好点。

答案 1 :(得分:6)

这就是我所做的。

NSString *myString = @"{53} balloons";
NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:@"{}"];
NSArray *splitString = [myString componentsSeparatedByCharactersInSet:delimiters];
NSString *substring = [splitString objectAtIndex:1];

子串是53。

答案 2 :(得分:2)

您可以使用正则表达式来获取大括号之间的数字。这可能看起来有点复杂,但优点是它会找到多个数字,而数字的位置并不重要。

Swift 4.2:

let searchText = "{53} balloons {12} clowns {123} sparklers"
let regex = try NSRegularExpression(pattern: "\\{(\\d+)\\}", options: [])
let matches = regex.matches(in: searchText, options: [], range: NSRange(searchText.startIndex..., in: searchText))
matches.compactMap { Range($0.range(at: 1), in: searchText) }
       .forEach { print("Number: \(searchText[$0])") }

<强>目标-C:

NSString *searchText = @"{53} balloons {12} clowns {123} sparklers";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{(\\d+)\\}" 
                                                                       options:0 
                                                                         error:nil];

NSArray *matches = [regex matchesInString:searchText 
                                  options:0 
                                    range:NSMakeRange(0, searchText.length)];

for (NSTextCheckingResult *r in matches)
{
    NSRange numberRange = [r rangeAtIndex:1];
    NSLog(@"Number: %@", [searchText substringWithRange:numberRange]);
}

这将打印出来:

Number: 53
Number: 12
Number: 123

答案 3 :(得分:1)

试试这段代码。

 NSString *myString = @"{53} balloons";
 NSString *value = [myString substringWithRange:NSMakeRange(1,2)];

答案 4 :(得分:1)

对于Swift 2.1: -

var start = strData?.rangeOfString("{")
var end = strData?.rangeOfString("}")
if (start!.location != NSNotFound && end!.location != NSNotFound && end!.location > start!.location) {
    var betweenBraces = strData?.substringWithRange(NSMakeRange(start!.location + 1, end!.location-(start!.location + 1)))
    print(betweenBraces)
}

答案 5 :(得分:0)

我想,你正在寻找NSScanner课程,至少如果你正在解决一般情况。看看苹果documentation

答案 6 :(得分:0)

搜索“{”和“}”的位置。 在这些索引之间取一个子串。

答案 7 :(得分:0)

检查任意数量的数据:

  NSString *str = @"{53} balloons";
    NSArray* strary = [str componentsSeparatedByString: @"}"];
    NSString* str1 = [strary objectAtIndex: 0];
    NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"{" withString:@""];
    NSLog(@"number = %@",str2);

另一种方法是

NSString *tmpStr = @"{53} balloons";
NSRange r1 = [tmpStr rangeOfString:@"{"];
NSRange r2 = [tmpStr rangeOfString:@"}"];
NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
NSString *subString = [tmpStr substringWithRange:rSub];

答案 8 :(得分:0)

对于Swift 4.2:

if let r1 = string.range(of: "{")?.upperBound,
let r2 = string.range(of: "}")?.lowerBound {
    print (String(string[r1..<r2]))
}

答案 9 :(得分:-1)

如果你不知道会有多少位数,但是你知道它总是用花括号括起来试试这个:

NSString *myString = @"{53} balloons";
NSRange startRange = [myString rangeOfString:@"{"];
NSRange endRange = [myString rangeOfString:@"}"];
if (startRange.location != NSNotFound && endRange.location != NSNotFound && endRange.location > startRange.location) {
    NSString *value = [myString substringWithRange:NSMakeRange(startRange.location,endRange.ocation - startRange.location)];
}

虽然可能有更有效的方法。