ios - 关于制作自定义 NSString

标签 ios nsstring substring

我有很多字符串,这里有 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/

相关文章:

Perl - 在变量中查找多个子字符串?

php - CodeIgniter 在数据库查询中插入撇号

ios - 如何删除我的 NSString 中带有括号 () 的任何子字符串?

ios - 用户界面测试 : Simulate a shake gesture with Swift

javascript - React Native 相当于 mousedown

ios - 在 Swift/IOS 中获取实际 View 大小

objective-c - 将字符串转换为 "Method"- 用于类方法调用

ios - NSMutableData 到 NSString

MySQL 在 TEXT 列中搜索子字符串

ios - iOS 中的 xmpp IQ XML 解析