如何检查字符串是否包含空格 [英] How to check whether a string contains white spaces

查看:235
本文介绍了如何检查字符串是否包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查字符串是否在字符之间包含空格?

How to check whether a string contains whitespaces in between characters?

推荐答案

使用rangeOfCharactersFromSet :

NSString *foo = @"HALLO WELT";
NSRange whiteSpaceRange = [foo rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
if (whiteSpaceRange.location != NSNotFound) {
    NSLog(@"Found whitespace");
}

注意:这也会在字符串的开头或结尾找到空格。如果你不想首先修剪字符串......

note: this will also find whitespace at the beginning or end of the string. If you don't want this trim the string first...

NSString *trimmedString = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSRange whiteSpaceRange = [trimmedString rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];

这篇关于如何检查字符串是否包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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