How to display each individual word of a string

hi all,
How can I display each word of a string individual? I wrote a loop, but it does not work.

1 Comment

How are the "words" separated? What about commas and full stops?

Sign in to comment.

Answers (3)

Use
my_str = strsplit('This is a test');
for ii = 1:length(my_str)
display(my_str{ii});
end
str='How to display each individual word of a string'
out=regexp(str,' ','split')
word_loc = find(sentence==' ' | sentence =='.');
count = size(word_loc,2);
words = cell(count,1);
prev=1;
for i=1:count
words{i}= sentence(prev:word_loc(i)-1);
prev = word_loc(i)+1;
end

1 Comment

The difficulty with this is that it splits numbers like 3.14 and it splits abbreviations like "e.g."

Sign in to comment.

Categories

Find more on Text Analytics Toolbox in Help Center and File Exchange

Asked:

Bob
on 5 Feb 2014

Commented:

on 26 Dec 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!