How to separate string by space using Objective-C?

前端 未结 5 2149
清歌不尽
清歌不尽 2021-01-30 16:16

Assume that I have a String like this:

hello world       this may     have lots   of sp:ace or little      space

I would like to seperate this

5条回答
  •  长情又很酷
    2021-01-30 16:59

    I'd suggest a two-step aproach:

    NSArray *wordsAndEmptyStrings = [yourLongString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSArray *words = [wordsAndEmptyStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];
    

提交回复
热议问题