从Objective-C中的字符串中删除空格 [英] Remove whitespace from string in Objective-C

查看:108
本文介绍了从Objective-C中的字符串中删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个字符串.有些在开始时有一个空格,有些则没有.我想检查字符串是否以空格开头,如果是,请将其删除.

I have a couple of strings. Some have a whitespace in the beginning and some not. I want to check if a string begins with a whitespace and if so remove it.

推荐答案

NSString类中有解决该问题的方法.检查stringByTrimmingCharactersInSet:(NSCharacterSet *)set.您应该使用[NSCharacterSet whitespaceCharacterSet]作为参数:

There is method for that in NSString class. Check stringByTrimmingCharactersInSet:(NSCharacterSet *)set. You should use [NSCharacterSet whitespaceCharacterSet] as parameter:

NSString *foo = @" untrimmed string ";
NSString *trimmed = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

这篇关于从Objective-C中的字符串中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆