我想从字符串的末尾字符串字符串,是否有任何字符串的 api 函数只能从字符串的结尾删除空格和换行符。
我编写了手动代码来从字符串末尾搜索字符并删除空格和换行符,但这可能会减慢进程。
需要API函数..
提前致谢
试试这个可能对你有帮助
-(NSString *)removeEndSpaceFrom:(NSString *)strtoremove{
NSUInteger location = 0;
unichar charBuffer[[strtoremove length]];
[strtoremove getCharacters:charBuffer];
int i = 0;
for ( i = [strtoremove length]; i >0; i--){
if (![[NSCharacterSet whitespaceCharacterSet] characterIsMember:charBuffer[i - 1]]){
break;
}
}
return [strtoremove substringWithRange:NSMakeRange(location, i - location)];
}
和
NSString *string = @" this text has spaces before and after ";
NSString *trimmedString = [self removeEndSpaceFrom:string];
NSLog(@"%@",trimmedString);