Function to capitalize first letter in each word in string but forces all other letters to be lowercase
Ältere Kommentare anzeigen
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 Kommentare
Azzi Abdelmalek
am 23 Nov. 2013
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
am 24 Nov. 2013
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
am 24 Nov. 2013
Bearbeitet: Azzi Abdelmalek
am 24 Nov. 2013
Olivia, to add a comment, click on [comment on this answer] under an answer of your choice
Olivia
am 24 Nov. 2013
Verschoben: Walter Roberson
am 24 Aug. 2024
Adoniram
am 6 Jan. 2019
Verschoben: Walter Roberson
am 24 Aug. 2024
thanks!
Akzeptierte Antwort
Weitere Antworten (2)
Image Analyst
am 24 Nov. 2013
Bearbeitet: dpb
am 1 Jan. 2017
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 Kommentar
Image Analyst
am 6 Jan. 2019
Bearbeitet: Image Analyst
am 24 Aug. 2024
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
am 23 Nov. 2013
Bearbeitet: per isakson
am 24 Nov. 2013
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 Kommentare
Stephen23
am 17 Mär. 2015
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
am 21 Aug. 2020
Hi Stephen,
This is great! Can you help me understand how '${upper($1)}' works?
Thanks
goc3
am 24 Aug. 2024
@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
am 24 Aug. 2024
@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.
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!