gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:08 24 4
gpt4 key购买 nike

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

'Superior Temporal Gyrus' => 'STG'

最佳答案

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

...你可以使用函数 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 values对于大写字母:

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 , FIND , 和 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

... 或者您可以使用 logical indexing 修改以上内容避免调用 FIND :

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


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

...你可以使用函数REGEXP :

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

关于regex - 如何从 MATLAB 中的字符串创建首字母缩略词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3038768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com