forked from anne-urai/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapitalize.m
28 lines (24 loc) · 863 Bytes
/
capitalize.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function S=capitalize(S,skipset)
%function S=capitalize(S)
% Capitalize the first character in every word (excluding short words) in input
% string S, or in every word in every string in input cell array S. The first word
% is always capitalized.
%
% Default set of words to skip is {'a','an','and','or','in','the'}
%
% function S=capitalize(S,skipset)
% User-supplied cell array of words to skip
if nargin<2
skipset={'a','an','and','or','in','the'};
end
skipset=lower(skipset);
if iscell(S)
S=cellfun(@(x) capitalize(x, skipset),S,'uniformoutput',false);
else
skip=0;
Sw=regexp(S,'[ \t]','split');
skip(2:length(Sw))=ismember(lower(Sw(2:end)),skipset); %skip(1) is always 0
Sw(~skip)=cellfun(@(x) [upper(x(1)) x(2:end)],Sw(~skip),'uniformoutput',false);
S=sprintf('%s ',Sw{:});
S(end)='';
end