我有一个字符串,我试图获得前2个字符和字符串的其余部分.这就是我试图这样做的方式:
NSString *textAll = [arrayOfMessages objectAtIndex:indexPath.row]; NSString *textMessage = [textAll substringFromIndex:2]; NSString *textType = [textAll substringToIndex:1];
textAll有这样的形式:消息本身…..
textType应该返回’HE’
textMessage应该返回’消息本身…..’
Textmessage现在给我的消息,但我似乎无法获得textType …
解决方法
要获取字符串的前两个字符,您需要:
NSString *textType = [textAll substringToIndex:2]; // <-- 2,not 1
从文档:
substringToIndex:
Returns a new string containing the characters of
the receiver up to,but not including,the one at a given index.
(注意,该方法需要字符串长度至少为2个字符,否则会引发异常.)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。