Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

ios - What is the most efficient way to count number of word in NSString without using regex?

I am a bit new to Objective C and was wondering if there is a better way to count words in a string.

ie:

NSString *str = @"this is a string";

// return should be 4 words ..

The way I now how to do it is by breaking the string into an array of words space (' ') character and count the array.

Any advise will be appreciated! Thanks!! :)

EDIT: For those of you who came here looking for answer; I found a similar post with an excellent reply.

How to count words within a text string?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two ways that don't involve collecting an array of words, and should be smarter than just breaking on spaces:

I would use one of these, even if I did want to collect or otherwise use the words.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...