0 matlab
我试图将字母表中只有三个字母单词的第一个和最后一个字母大写.到目前为止,我已经尝试过了
spaces = strfind(str, ' ');
spaces = [0 spaces];
lw = diff(spaces);
lw3 = find(lw ==4);
a3 = lw-1;
b3 = spaces(a3+1);
b4 = b3 + 2 ;
str(b3) = upper(str(b3));
str(b4) = upper(str(b4);
Run Code Online (Sandbox Code Playgroud)
我们必须找到3个字母单词的第一个位置,这就是前4行代码是什么,然后其他人试图得到它以便找到第一个和最后一个字母的位置,然后将它们大写?
我会使用正则表达式来识别3个字母的单词,然后regexprep结合使用匿名函数来执行大小写转换.
str = 'abcd efg hijk lmn';
% Custom function to capitalize the first and last letter of a word
f = @(x)[upper(x(1)), x(2:end-1), upper(x(end))];
% This will match 3-letter words and apply function f to them
out = regexprep(str, '\<\w{3}\>', '${f($0)}')
% abcd EfG hijk LmN
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
449 次 |
| 最近记录: |