检查 NSMakeRange 是否能够获取范围
我目前正在使用下面的代码来获取子字符串
NSString *s1 = [stringName substringWithRange: NSMakeRange(0, 2)];
NSString *s2 = [stringName substringWithRange: NSMakeRange(2, 4)];
NSString *s3 = [stringName substringWithRange: NSMakeRange(6, [stringName length])];
,这里如何检查范围是否有效,因为我无法验证字符串的长度,因为它可能会随时间而变化。请帮助我解决这个问题..
I am currently using the below code to take the substring
NSString *s1 = [stringName substringWithRange: NSMakeRange(0, 2)];
NSString *s2 = [stringName substringWithRange: NSMakeRange(2, 4)];
NSString *s3 = [stringName substringWithRange: NSMakeRange(6, [stringName length])];
Here, how to check the range is valid or not because i am not able to length of string to validate because it may differ on time. Please help me in this issue..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于
s3
,您从位置开始并要求与字符串中的字符数完全相同。但由于您从位置 6 开始,因此您要求的 6 个字符太多了。你需要做:For
s3
, you're starting at position and ask for exactly as many characters as there are in the string. But since you're starting at position 6, you're asking for 6 characters too many. You need to do:正如苹果文档所述..
substringWithRange:
返回一个字符串对象,其中包含给定范围内的接收者字符。
aRange
每次构造 NSMakeRange() 时,您都需要检查您在字符串长度内指定的范围是否在...通过使用一些
if 和 else
条件As the apple documentation says ..
substringWithRange:
Returns a string object containing the characters of the receiver that lie within a given range.
aRange
You need to check every time when you construct an NSMakeRange(), whether the Range you specified within the length of your String OR not ... by Using some
if and else
condition