How to Change the first letter of a string to capital case and the rest to lowercase
78 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
sydney salvador
am 13 Dez. 2020
Kommentiert: Walter Roberson
am 5 Jun. 2024
impropermsg = input('Enter the sentence : \n', 's');
function propermsg = changemsg(impropermsg)
propermsg = strcat(upper(impropermsg(1)),lower(impropermsg(2:end)));
end
% The user can input but nothing happens
3 Kommentare
Les Beckham
am 13 Dez. 2020
You created a function that looks like it will do what you want but (at least in the code you posted) you never called the function.
You could just eliminate the 2nd and 4th lines of this code and you will have a simple two line script that does what you want (with the answer being in the propermsg string). If you want the result to be displayed, just take the semicolon off of the end of the propermsg = ... line.
Akzeptierte Antwort
Image Analyst
am 13 Dez. 2020
Make a script called something like test.m, and put this into it:
impropermsg = input('Enter the sentence : \n', 's');
% Now call the function:
propermsg = changemsg(impropermsg);
fprintf('The result is "%s"\n', propermsg);
function propermsg = changemsg(impropermsg)
propermsg = strcat(upper(impropermsg(1)),lower(impropermsg(2:end)));
end
0 Kommentare
Weitere Antworten (2)
mahmoud abdelaal
am 3 Okt. 2022
Verschoben: Image Analyst
am 3 Okt. 2022
word='si'
new=replace(word,word(1),upper(word(1)))
1 Kommentar
Image Analyst
am 3 Okt. 2022
That doesn't replace the rest with lower case like requested:
word='siUPPERCASE'
new=replace(word,word(1),upper(word(1)))
You'd need to add this line:
new = replace(new, word(2:end), lower(word(2:end)))
Toshiaki Takeuchi
am 4 Jun. 2024
Bearbeitet: Walter Roberson
am 5 Jun. 2024
str = "test";
pat = lineBoundary("start")+lettersPattern(1);
new_str = replace(str,pat,(upper(extract(str,pat))))
1 Kommentar
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!