如何显示字符串中的每个单词? [英] How to display each individual word of a string?

查看:148
本文介绍了如何显示字符串中的每个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示字符串中的每个单词?我写了一个循环,但是没有用.

How can I display each individual word of a string? I wrote a loop, but it doesn't work.

推荐答案

尝试 strsplit ,默认情况下按空格分隔:

Try strsplit, which by default splits by white-space:

>> str='How to display each individual word of a string';
>> words = strsplit(str);
>> words.' %' each word is in a cell (words{1} is first word)
ans = 
    'How'
    'to'
    'display'
    'each'
    'individual'
    'word'
    'of'
    'a'
    'string'

或者,如果您更喜欢 regexp ,则可以应用很少使用的 'split'关键字:

Or if you prefer regexp, you can apply the rarely-used 'split' keyword:

>> words = regexp(str,'[\s]','split')
words = 
    'How'    'to'    'display'    'each'    'individual'    'word'    'of'    'a'    'string'

这篇关于如何显示字符串中的每个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆