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!