简体   繁体   English

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

[英]How can I create an acronym from a string in MATLAB?

Is there an easy way to create an acronym from a string in MATLAB? 有没有一种简单的方法可以在MATLAB中创建字符串的首字母缩略词? For example: 例如:

'Superior Temporal Gyrus' => 'STG'

If you want to put every capital letter into an abbreviation... 如果你想把每个大写字母都写成缩写......

... you could use the function REGEXP : ...你可以使用函数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 : ...或者你可以使用UPPERISSPACE函数:

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: ...或者您可以使用大写字母的ASCII / UNICODE值

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 : ...你可以使用函数REGEXP

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

... or you could use the functions STRTRIM , FIND , and ISSPACE : ...或者您可以使用STRTRIMFINDISSPACE函数

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 : ...或者您可以使用逻辑索引修改上述内容以避免调用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 : ...你可以使用REGEXP函数:

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

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 请注意,字符串必须在句子中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM