本文介绍了如何获取大括号之间的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串.

I have a string as this.

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

如何获取子字符串 53 ?

How do I get the substring 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 - 好点.

edit: Added range check, thx to Keab42 - good point.

这篇关于如何获取大括号之间的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:11