7
A
回答
21
因此,使用字符串
str='the rain in spain falls mainly on the plain.'
只需使用Matlab中的正则表达式替换功能,regexprep
regexprep(str,'(\<[a-z])','${upper($1)}')
ans =
The Rain In Spain Falls Mainly On The Plain.
的\<[a-z]
每个单词的第一个字符相匹配,您可以转换使用${upper($1)}
大写
这也可以使用\<\w
来匹配每个单词开头的字符。
1
负载:
str = 'the rain in Spain falls mainly on the plane'
spaceInd = strfind(str, ' '); % assume a word is preceded by a space
startWordInd = spaceInd+1; % words start 1 char after a space
startWordInd = [1, startWordInd]; % manually add the first word
capsStr = upper(str);
newStr = str;
newStr(startWordInd) = capsStr(startWordInd)
更多优雅/复杂 - 单元阵列,textscan和cellfun对于这种事情非常有用:
str = 'the rain in Spain falls mainly on the plane'
function newStr = capitals(str)
words = textscan(str,'%s','delimiter',' '); % assume a word is preceded by a space
words = words{1};
newWords = cellfun(@my_fun_that_capitalizes, words, 'UniformOutput', false);
newStr = [newWords{:}];
function wOut = my_fun_that_capitalizes(wIn)
wOut = [wIn ' ']; % add the space back that we used to split upon
if numel(wIn)>1
wOut(1) = upper(wIn(1));
end
end
end
2
由于Matlab自带的build in Perl,对于每一个复杂的字符串或文件处理任务都可以使用Perl脚本。所以,你也许可以用这样的:
[result, status] = perl('capitalize.pl','the rain in Spain falls mainly on the plane')
其中capitalize.pl是一个Perl脚本如下:
$input = $ARGV[0];
$input =~ s/([\w']+)/\u\L$1/g;
print $input;
Perl代码从this堆栈溢出问题被采取。
1
str='the rain in spain falls mainly on the plain.' ;
for i=1:length(str)
if str(i)>='a' && str(i)<='z'
if i==1 || str(i-1)==' '
str(i)=char(str(i)-32); % 32 is the ascii distance between uppercase letters and its lowercase equivalents
end
end
end
少ellegant高效,更易读和可维护性。
相关问题
- 1. 首字母大写的字符串首字母大写
- 2. 字符串的首字母大写
- 3. 首字母大写从MySQL与PHP/jQuery拉的每个单词的首字母
- 4. 在windows phone上字符串中的每个单词的首字母大写
- 5. 首字母大写
- 6. 大写首字母
- 7. 在vim中选择每个单词的首字母大写
- 8. 仅字符串首字母大写java
- 9. 在Drupal 7中输入每个单词的首字母大写字母
- 10. 为字符串中的每个单词首字母大写。本地化问题
- 11. CKeditor中首字母大写首字母缩写
- 12. MySQL中每个单词(utf8)的大写首字母
- 13. 将列中每个单词的首字母大写Python
- 14. 如何自动大写C#中每个单词的首字母?
- 15. 每个句子的首字母大写
- 16. 大写首字母的姓
- 17. 将字符串中的第一个字符大写首字母大写
- 18. 首字母大写的字符
- 19. 使用增强库大写句子中每个单词的首字母大写
- 20. MySQL - 每个单词的首字母大写
- 21. 返回每个单词的首字母大写
- 22. Titanium - TextField键盘类型为单词首字母大写字母
- 23. 首字母应大写?
- 24. 首字母大写。 MySQL
- 25. 大写首字母只
- 26. 作首字母大写
- 27. SQL:仅首字母大写
- 28. 用大写字母填满句子中的每个单词的首字母大写
- 29. python首字母大写只有大写
- 30. 使用javascript大写首字母大写
是否可以接受其中一个答案,谢谢。 – Morgan 2014-12-08 10:06:35