How do I use the strsplit function to split a sentence into a string of letters?

y = strsplit(sentence,{''},'CollapseDelimiters',true);
I need to find something to set as a delimiter that will split the sentence up into characters.

回答 (3 件)

Stephen23
Stephen23 2018 年 11 月 6 日
編集済み: Stephen23 2018 年 11 月 6 日
It is not very clear what exactly you want to achieve (you did not give any input and output examples), but here are some possibilities. To split a sentence into words:
regexp(sentence, '\s+', 'split')
To split the sentence into characters, ignoring the spaces:
regexp(sentence, '\S', 'match')
If you simple want every character in its own cell, including spaces, then this works:
num2cell(sentence)

3 件のコメント

Zack Rabas
Zack Rabas 2018 年 11 月 6 日
that splits each word into a cell. I need to split each letter into a cell, I'm not sure how to do that
Stephen23
Stephen23 2018 年 11 月 6 日
"I need to split each letter into a cell, I'm not sure how to do that"
That is what my second example does. It is not clear what you want to do with space though.
Image Analyst
Image Analyst 2018 年 11 月 6 日
But WHY does it need to be a cell array? Is it homework where it requires it? Why not just leave it as a vector like I showed?

サインインしてコメントする。

Image Analyst
Image Analyst 2018 年 11 月 6 日
A sentence is ALREADY an array of individual characters.:
sentence = 'Hello World';
for k = 1 : length(sentence)
disp(sentence(k))
end
You'll see
H
e
l
l
o
W
o
r
l
d
No need for strsplit() or cell arrays. Just use an index and treat it like any other vector.
Star Strider
Star Strider 2018 年 11 月 6 日
Another option:
sentence = 'This is a sentence';
y = num2cell(sentence); % Cell Array Of Letters With Spaces
y_nospace = y(~strcmpi(y, ' ')); % ... And Without Spaces

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

タグ

質問済み:

2018 年 11 月 6 日

コメント済み:

2018 年 11 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by