本文介绍了如何在MATLAB中从字符串创建首字母缩写词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种简便的方法可以从MATLAB中的字符串创建首字母缩写词?例如:
Is there an easy way to create an acronym from a string in MATLAB? For example:
'Superior Temporal Gyrus' => 'STG'
推荐答案
如果您想将每个大写字母都放在缩写中...
......,您可以使用功能 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
...,或者您可以改为使用 ASCII/UNICODE值大写字母:
... 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)
......,您可以使用功能 REGEXP :
... you could use the function REGEXP:
abbr = str(regexp(str,'\w+')); %# Get the starting letter of each word
...,或者您可以使用 STRTRIM ,查找和 ISSPACE :
... 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)]);
......,您可以使用功能 REGEXP :
... you can use the function REGEXP:
abbr = str(regexp(str,'\<[A-Z]\w*'));
这篇关于如何在MATLAB中从字符串创建首字母缩写词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!