matlab - Capitalizing only the first letters without changing any numbers or punctuation -
i modify string have make first letter capitalized , other letters lower cased, , else unchanged.
i tried this:
function new_string=switchcase(str1) %str1 represents given string containing word or phrase str1lower=lower(str1); spaces=str1lower==' '; caps1=[true spaces]; %we want first letter , letters after space capital. strnew1=str1lower; strnew1(caps1)=strnew1(caps1)-32; end
this function works nicely if there nothing other letter after space. if have else example:
str1='wow ! ~code~ works !!'
then gives new_string = 'wow ^code~ works !' however, has give (according requirement),
new_string = 'wow! ~code~ works !'
i found code has similarity problem. however, ambiguous. here can ask question if don't understand.
any appreciated! thanks.
interesting question +1.
i think following should fulfil requirements. i've written example sub-routine , broken down each step obvious i'm doing. should straightforward condense function here.
note, there clever way single regular expression, i'm not regular expressions :-) doubt regular expression based solution run faster i've provided (but happy proven wrong).
%# example string str1 ='wow ! ~code~ works !!'; %# convert case lower str1 = lower(str1); %# convert ascii str1 = double(str1); %# find index of locations after spaces i1 = logical([0, (str1(1:end-1) == 32)]); %# eliminate locations don't contain lower-case characters i1 = logical(i1 .* ((str1 >= 97) & (str1 <= 122))); %# check manually if first location contains lower-case character if str1(1) >= 97 && str1(1) <= 122; i1(1) = true; end; %# adjust appropriate characters in ascii form str1(i1) = str1(i1) - 32; %# convert result string str1 = char(str1);
Comments
Post a Comment