我有很多字符串,这里有 3 个:
1. example old
14. example new
234. example 45
我想排除第一个数字并将其设为:
example old
example new
example 45
我做的是:
NSString *fixed1 = [str substringWithRange:NSMakeRange(3, [str length]-3)];
NSString *fixedStr = [fixed1 stringByReplacingOccurrencesOfString:@". " withString:@""];
我得到的是:
example old
example new
example 45
问题是“example new”开头的空格。我不知道如何删除它,如果我尝试这样做:
NSString *fixed1 = [str substringWithRange:NSMakeRange(3, [str length]-3)];
NSString *fixed2 = [fixed1 stringByReplacingOccurrencesOfString:@". " withString:@""];
NSString *fixedStr = [fixed2 stringByReplacingOccurrencesOfString:@" " withString:@""];
我明白了:
exampleold
examplenew
example45
我快要被这个问题搞疯了,请问有人有一个好的简单的想法来解决这个问题吗?
谢谢!
最佳答案
你可以使用
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
以 [NSCharacterSet whitespaceCharacterSet]
作为参数来去除开头和结尾的空格。
关于ios - 关于制作自定义 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12709959/