本文介绍了从 NSString 的末尾修剪空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除字符串末尾的空格.我怎样才能做到这一点?示例:如果字符串是 "Hello "
它必须变成 "Hello"
I need to remove spaces from the end of a string. How can I do that?Example: if string is "Hello "
it must become "Hello"
推荐答案
摘自这里的答案:https://stackoverflow.com/a/5691567/251012
- (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet {
NSRange rangeOfLastWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet]
options:NSBackwardsSearch];
if (rangeOfLastWantedCharacter.location == NSNotFound) {
return @"";
}
return [self substringToIndex:rangeOfLastWantedCharacter.location+1]; // non-inclusive
}
这篇关于从 NSString 的末尾修剪空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!