6

我有一个这样的字符串。

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

如何获取子字符串53

4

10 回答 10

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

编辑:添加范围检查,感谢 Keab42 - 好点。

于 2013-02-19T14:03:41.170 回答
6

这就是我所做的。

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

子字符串是 53。

于 2013-02-20T08:03:18.133 回答
2

您可以使用正则表达式来获取大括号之间的数字。它可能看起来有点复杂,但好处是它会找到多个数字并且数字的位置无关紧要。

斯威夫特 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
于 2014-03-07T11:08:24.777 回答
1

试试这个代码。

 NSString *myString = @"{53} balloons";
 NSString *value = [myString substringWithRange:NSMakeRange(1,2)];
于 2013-02-19T13:58:29.367 回答
1

对于 Swift 4.2:

if let r1 = string.range(of: "{")?.upperBound,
let r2 = string.range(of: "}")?.lowerBound {
    print (String(string[r1..<r2]))
}
于 2018-10-28T13:04:48.423 回答
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)
}
于 2016-03-30T11:35:24.747 回答
0

我想,您正在寻找NSScanner课程,至少如果您正在解决一般情况。查看 Apples文档

于 2013-02-19T14:02:24.280 回答
0

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

于 2013-02-19T14:04:29.760 回答
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];
于 2013-02-19T14:06:56.627 回答
-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)];
}

不过,可能有一种更有效的方法来做到这一点。

于 2013-02-19T14:05:49.517 回答