我有一个这样的字符串。
NSString *myString = @"{53} balloons";
如何获取子字符串53
?
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 - 好点。
这就是我所做的。
NSString *myString = @"{53} balloons";
NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:@"{}"];
NSArray *splitString = [myString componentsSeparatedByCharactersInSet:delimiters];
NSString *substring = [splitString objectAtIndex:1];
子字符串是 53。
您可以使用正则表达式来获取大括号之间的数字。它可能看起来有点复杂,但好处是它会找到多个数字并且数字的位置无关紧要。
斯威夫特 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
试试这个代码。
NSString *myString = @"{53} balloons";
NSString *value = [myString substringWithRange:NSMakeRange(1,2)];
对于 Swift 4.2:
if let r1 = string.range(of: "{")?.upperBound,
let r2 = string.range(of: "}")?.lowerBound {
print (String(string[r1..<r2]))
}
对于 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)
}
我想,您正在寻找NSScanner
课程,至少如果您正在解决一般情况。查看 Apples文档。
搜索“{”和“}”的位置。在这些索引之间获取子字符串。
检查任意数量的数据:
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];
如果你不知道会有多少位数,但你知道它总是用花括号括起来,试试这个:
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)];
}
不过,可能有一种更有效的方法来做到这一点。