How to check whether a string contains white spaces

后端 未结 2 1406
北海茫月
北海茫月 2021-02-05 05:49

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

2条回答
  •  猫巷女王i
    2021-02-05 06:28

    You can also follow these steps:

    NSArray *componentsSeparatedByWhiteSpace = [testString componentsSeparatedByString:@" "];
    

    If there is any whitespace in your string, then it will separate those and store different components in the array. Now you need to take the count of array. If count is greater than 1, it means there are two components, i.e, presence of white space.

    if([componentsSeparatedByWhiteSpace count] > 1){
        NSLog(@"Found whitespace");
    }
    

提交回复
热议问题