Function to capitalize first letter in each word in string but forces all other letters to be lowercase
古いコメントを表示
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 件のコメント
Azzi Abdelmalek
2013 年 11 月 23 日
Olivia commented
Thanks for your help! I ran it and it worked :), however I need to be able to do this as a function! Any ideas? I'm still really new to coding!
Thanks again,
Olivia
Azzi Abdelmalek
2013 年 11 月 24 日
Olivia commented
That works! Thank you for your help! Could you please tell me how it works exactly? I understand that the string is set to lowercase and then the first letters of each word are indexed but I am not sure exactly how:
idx=regexp([' ' str],'(?<=\s+)\S','start')-1;
Thanks again :)
Olivia
Azzi Abdelmalek
2013 年 11 月 24 日
編集済み: Azzi Abdelmalek
2013 年 11 月 24 日
Olivia, to add a comment, click on [comment on this answer] under an answer of your choice
Olivia
2013 年 11 月 24 日
移動済み: Walter Roberson
2024 年 8 月 24 日
Adoniram
2019 年 1 月 6 日
移動済み: Walter Roberson
2024 年 8 月 24 日
thanks!
採用された回答
その他の回答 (2 件)
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 件のコメント
Now you can use strsplit() to split apart a sentence into individual words.
s='this iS the iNpUt StrinG'
individualWords = strsplit(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
new_s = strtrim(new_s); % Get rid of leading space.
fprintf('Here is the new, output string:\n%s', new_s);
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
2015 年 3 月 17 日
This can be used in a nice one-line solution:
>> str = 'thIs IS a tEST StRiNg! wITH TWO sEntences.';
>> regexprep(lower(str),'(\<[a-z])','${upper($1)}')
ans =
This Is A Test String! With Two Sentences.
Raghu Jagannathan
2020 年 8 月 21 日
Hi Stephen,
This is great! Can you help me understand how '${upper($1)}' works?
Thanks
goc3
2024 年 8 月 24 日
@goc3: strictly speaking [a-z] only matches Latin letters without diacritics, not every letter that can be capitalised (Latin with diacritics, Greek, Cyrillic, ...). What is the canonical MATLAB regular expression to match lower-case (or upper case) characters? Here an example using České dráhy:
T = 'české dráhy';
regexprep(lower(T),'(\<[a-z])','${upper($1)}') % fails
X = char(0:65535);
Y = X(isstrprop(X,'lower'))
regexprep(lower(T),sprintf('(\\<[%s])',Y),'${upper($1)}') % works
goc3
2024 年 8 月 24 日
@Stephen23: are you referring to \w? That also matches the underscore and digits, though it does work in this case.
Latin-only letters are sufficient for my purposes.
I thought I would ask the AI Chat Playground if it could come up with a solution. Its first attempt used the title() function, which obviously does NOT work. I told it that its "solution" created a figure and to attempt it again, after which it provided another "solution" that did not work. I again told it that what it provided did not work, after which it gave me the regexprep solution you provided above, though without the lower() part to ensure all non-first letters are not capitalized.
I also checked the functions listing for the Text Analytics toolbox; as far as I can tell, it does not have a built-in function to apply title case.
@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.
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!