如何在 MATLAB 中从字符串创建首字母缩略词?

发布于 2024-09-05 09:48:55 字数 109 浏览 2 评论 0原文

有没有一种简单的方法可以在 MATLAB 中从字符串创建首字母缩略词?例如:

'Superior Temporal Gyrus' => 'STG'

Is there an easy way to create an acronym from a string in MATLAB? For example:

'Superior Temporal Gyrus' => 'STG'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-09-12 09:48:55

如果你想把每个大写字母变成缩写......

你可以使用函数 REGEXP

str = 'Superior Temporal Gyrus';  %# Sample string
abbr = str(regexp(str,'[A-Z]'));  %# Get all capital letters

...或者您可以使用函数 UPPERISSPACE

abbr = str((str == upper(str)) & ~isspace(str));  %# Compare str to its uppercase
                                                  %#   version and keep elements
                                                  %#   that match, ignoring
                                                  %#   whitespace

...或者您可以使用 ASCII大写字母的 /UNICODE 值

abbr = str((str <= 90) & (str >= 65));  %# Get capital letters A (65) to Z (90)

如果您想将单词开头的每个字母放入缩写中...

...您可以使用函数 REGEXP

abbr = str(regexp(str,'\w+'));  %# Get the starting letter of each word

...或者您可以使用函数 STRTRIM查找,以及 ISSPACE:

str = strtrim(str);  %# Trim leading and trailing whitespace first
abbr = str([1 find(isspace(str))+1]);  %# Get the first element of str and every
                                       %#   element following whitespace

...或者您可以使用 逻辑索引以避免调用 FIND

str = strtrim(str);  %# Still have to trim whitespace
abbr = str([true isspace(str)]);

如果您想将单词开头的每个大写字母放入缩写中.. ...

您可以使用函数 REGEXP :

abbr = str(regexp(str,'\<[A-Z]\w*'));

If you want to put every capital letter into an abbreviation...

... you could use the function REGEXP:

str = 'Superior Temporal Gyrus';  %# Sample string
abbr = str(regexp(str,'[A-Z]'));  %# Get all capital letters

... or you could use the functions UPPER and ISSPACE:

abbr = str((str == upper(str)) & ~isspace(str));  %# Compare str to its uppercase
                                                  %#   version and keep elements
                                                  %#   that match, ignoring
                                                  %#   whitespace

... or you could instead make use of the ASCII/UNICODE values for capital letters:

abbr = str((str <= 90) & (str >= 65));  %# Get capital letters A (65) to Z (90)

If you want to put every letter that starts a word into an abbreviation...

... you could use the function REGEXP:

abbr = str(regexp(str,'\w+'));  %# Get the starting letter of each word

... or you could use the functions STRTRIM, FIND, and ISSPACE:

str = strtrim(str);  %# Trim leading and trailing whitespace first
abbr = str([1 find(isspace(str))+1]);  %# Get the first element of str and every
                                       %#   element following whitespace

... or you could modify the above using logical indexing to avoid the call to FIND:

str = strtrim(str);  %# Still have to trim whitespace
abbr = str([true isspace(str)]);

If you want to put every capital letter that starts a word into an abbreviation...

... you can use the function REGEXP:

abbr = str(regexp(str,'\<[A-Z]\w*'));
千寻… 2024-09-12 09:48:55

谢谢,还有这个:

s1(regexp(s1, '[A-Z]', 'start'))

将返回字符串中由大写字母组成的缩写。请注意,字符串必须采用句子大小写

thanks, also this:

s1(regexp(s1, '[A-Z]', 'start'))

will return abbreviation consisting of capital letters in the string. Note the string has to be in Sentence Case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文