Capitalizing every word in a string

2 ビュー (過去 30 日間)
Samiha Shimla
Samiha Shimla 2019 年 1 月 17 日
コメント済み: Stephen23 2019 年 1 月 17 日
str='The bottom of the deep blue sea'
strN='The Bottom Of The Deep Blue Sea'
To convert the string 'str' to 'strN' is used:
regexprep(str,'(\<[a-z])','${upper($1)}')
Can someone please xeplain this? Another suggestion which did not work was:
regexprep(str,'(\<\w)','${upper($1)}')
Is there a simpler (it is ok if it is longer) way of doing this?
  1 件のコメント
Stephen23
Stephen23 2019 年 1 月 17 日
regexprep(str,'(\w)(\w+)','${upper($1)}$2')

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 1 月 17 日
編集済み: Jan 2019 年 1 月 17 日
I like simple Matlab code:
m = [false, isspace(str)];
m = m(1:length(str));
str(m) = upper(str(m))
By the way, tested with: '', ' ', 'a', 'aA', ' a', ' a ', ' ab c', ' ab c ' .
Some timings:
str0='The bottom of the deep blue sea';
tic
for k = 1:1e4
str = str0;
m = [false, isspace(str)];
m = m(1:length(str));
str(m) = upper(str(m));
end
toc
tic
for k = 1:1e4
str = str0;
str = regexprep(str,'(\<[a-z])','${upper($1)}');
end
toc
0.17 seconds % Simple Matlab
3.24 seconds % Powerful REGEXP

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by