Function to capitalize first letter in each word in string but forces all other letters to be lowercase

62 ビュー (過去 30 日間)
Does anyone know how to create a function which accepts a string and capitalizes the first letter in each word of the string, but also forces the other letters to be lowercase?
Any advice would be greatly appreciated!!
This is my attempt so far:
str=['this is a TEST'];
for i=1:length(str);
if str(1,i(1));
str= upper(str);
else str(1,i);
str= lower(str);
end
end
  5 件のコメント
Olivia
Olivia 2013 年 11 月 24 日
移動済み: Walter Roberson 2024 年 8 月 24 日
Thank you very much for the help!!
Adoniram
Adoniram 2019 年 1 月 6 日
移動済み: Walter Roberson 2024 年 8 月 24 日
thanks!

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 23 日
編集済み: Azzi Abdelmalek 2013 年 11 月 23 日
str='this is a tEST';
str=lower(str)
idx=regexp([' ' str],'(?<=\s+)\S','start')-1
str(idx)=upper(str(idx))
  4 件のコメント
Nayeb Hasin
Nayeb Hasin 2022 年 6 月 16 日
what if this was the input '99this is a TEST'?
Image Analyst
Image Analyst 2022 年 6 月 16 日
@Nayeb Hasin Just try it:
str='99this is a TEST';
strout=uplow(str)
str = '99this is a test'
idx = 1×4
1 8 11 13
str = '99this Is A Test'
Output argument "strout" (and possibly others) not assigned a value in the execution with "solution>uplow" function.
function strout=uplow(str)
str=lower(str)
idx=regexp([' ' str],'(?<=\s+)\S','start')-1
str(idx)=upper(str(idx))
end
I guess that error is not what you expected or wanted.
Try changing strout on the function line to str.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 11 月 24 日
編集済み: dpb 2017 年 1 月 1 日
If you want something a lot more intuitive and a lot simpler than regexp, you can use allwords(). I use allwords all the time - it's a very useful utility.
s='this iS the iNpUt StrinG'
individualWords = allwords(s) % Get the words separated individually.
% Capitalize the first letters
new_s = '';
for k = 1 : length(individualWords)
% Get the kth word.
thisWord = individualWords{k}
% Capitalize the first letter. Lower the subsequent letters.
capWord = [upper(thisWord(1)), lower(thisWord(2:end))]
% Concatenate to the new string.
new_s = [new_s, ' ', capWord]
end
message = sprintf('This is the new string: %s', new_s);
uiwait(helpdlg(message));
You'll probably find this code a lot easier to understand than if you use regexp - I know I do. Not as compact though.
  1 件のコメント
Image Analyst
Image Analyst 2019 年 1 月 6 日
編集済み: Image Analyst 2024 年 8 月 24 日
Now you can use strsplit() to split apart a sentence into individual words.
s='this iS the iNpUt StrinG'
s = 'this iS the iNpUt StrinG'
individualWords = strsplit(s) % Get the words separated individually.
individualWords = 1x5 cell array
{'this'} {'iS'} {'the'} {'iNpUt'} {'StrinG'}
% Capitalize the first letters
new_s = '';
for k = 1 : length(individualWords)
% Get the kth word.
thisWord = individualWords{k};
% Capitalize the first letter. Lower the subsequent letters.
capWord = [upper(thisWord(1)), lower(thisWord(2:end))];
% Concatenate to the new string.
new_s = [new_s, ' ', capWord];
end
new_s = strtrim(new_s); % Get rid of leading space.
fprintf('Here is the new, output string:\n%s', new_s);
Here is the new, output string: This Is The Input String

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


per isakson
per isakson 2013 年 11 月 23 日
編集済み: per isakson 2013 年 11 月 24 日
The help on regexprep (R2013a) includes this example
str = 'here are two sentences. neither is capitalized.';
expression = '(^|\.)\s*.';
replace = '${upper($0)}';
newStr = regexprep(str,expression,replace)
Try
str = ['this is a TEST'];
str = lower( str );
expression = '(^|\.)\s*.';
replace = '${upper($0)}';
newStr = regexprep(str,expression,replace)
However, every word should be capitalized. Change expression to include characters after space
expression = '(^|[\. ])\s*.';
  7 件のコメント
Stephen23
Stephen23 2024 年 8 月 24 日
編集済み: Stephen23 2024 年 8 月 24 日
"are you referring to \w?"
No. But I am curious, how are you using it to achieve the task the OP asked about?
goc3
goc3 2024 年 8 月 24 日
編集済み: goc3 2024 年 8 月 24 日
@Stephen23: In my case, all the letters are already lowercase and are only from the set [a-zA-Z_0-9].
If I were writing a utility function, the more comprehensive solution you provided in the more-recent comment would be preferred.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by