NSString - 删除最后一个空格后的所有字符

iPh*_*one 2 iphone objective-c nsstring

我的字符串是 @"Hello, I am working as an ios developer"

现在我想要删除所有字符 "ios"

最后我想删除最后一个空格字符后的所有字符.

我怎样才能做到这一点?

Bha*_*vin 9

示例代码:

NSString* str= @"Hello, I am working as an ios developer";

// Search from back to get the last space character
NSRange range= [str rangeOfString: @" " options: NSBackwardsSearch];

// Take the first substring: from 0 to the space character
NSString* finalStr = [str substringToIndex: range.location]; // @"Hello, I am working as an ios" 
Run Code Online (Sandbox Code Playgroud)