From: Fraser Dickson on
Hi I have a string with 3 words in it

"BLUE RED GREEN"

I want it all to be in lower case except the first letter of each word . i.e

"Blue Red Green"

I know how to convert it all to lower case but im unsure how to keep each of the first letters as capitals.

Thanks
From: us on
"Fraser Dickson" <fraser.dickson(a)gmail.com> wrote in message <hmr7hf$as1$1(a)fred.mathworks.com>...
> Hi I have a string with 3 words in it
>
> "BLUE RED GREEN"
>
> I want it all to be in lower case except the first letter of each word . i.e
>
> "Blue Red Green"
>
> I know how to convert it all to lower case but im unsure how to keep each of the first letters as capitals.
>
> Thanks

one of the solutions

s='BLUE RED GREEN';
r=regexprep(s,'(.)(\w+)(\s*)','$1${lower($2)}$3')
% r = Blue Red Green

us
From: Fraser Dickson on
Ah Thanks Us

Its nearly exactly what i want but is there a way to also get it to work when a word only has one letter in it i.e

s = " A GREEN CAT"

your code will produce

A green Cat

whereas i would want

A Green Cat

Thanks for your help
Fraser
From: us on
"Fraser Dickson" <fraser.dickson(a)gmail.com> wrote in message <hmtckb$o94$1(a)fred.mathworks.com>...
> Ah Thanks Us
>
> Its nearly exactly what i want but is there a way to also get it to work when a word only has one letter in it i.e
>
> s = " A GREEN CAT"
>
> your code will produce
>
> A green Cat
>
> whereas i would want
>
> A Green Cat
>
> Thanks for your help
> Fraser

one of the solutions

s=upper('a green cat a green cat');
r=regexprep(s,'([^\s])(\w+)(\s*)','$1${lower($2)}$3')
% r = A Green Cat A Green Cat

us
From: Jan Simon on
Dear Fraser!

> Hi I have a string with 3 words in it
> "BLUE RED GREEN"
>
> I want it all to be in lower case except the first letter of each word . i.e
> "Blue Red Green"
>
> I know how to convert it all to lower case but im unsure how to keep each of the first letters as capitals.

So how is your problem defined algorithmically?
All characters in lower case, but the characters following a space and the first character in upper case.

S = "BLUE RED GREEN";
S = lower(S);
ind = [1, findstr(S, ' ') + 1];
S(ind) = upper(S(ind));

This is more trivial then the nice REGEXP approach. It fails if the last character of S is a space.

Kind regards, Jan
 |  Next  |  Last
Pages: 1 2
Prev: Call by reference / references
Next: matlab function