-
Notifications
You must be signed in to change notification settings - Fork 2
/
caseconvert.m
202 lines (150 loc) · 5.76 KB
/
caseconvert.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function[data]=caseconvert(data,casetype)
%CASECONVERT Case converter.
%
% CASECONVERT(S,T) converts the case of string S to type T. S can be a
% single string, or a cell/char array of strings. Type T also can be a
% single string, or a cell/char array of strings with size(T)==size(S).
% Where applicable, output generally resembles that obtained using the
% 'Change Case' feature within Microsoft Word 2003.
%
% If T is a a cell/char array with size(T)==size(S), each value of T
% applies to each respective value of S.
%
% EXAMPLES:
%
% caseconvert('sample text','title') returns 'Sample Text'
% caseconvert('Sample Text','toggle') returns 'sAMPLE tEXT'
%
% caseconvert({'new york' 'california'},'randomized')
% returns {'neW yoRk' 'CAlIFORnIA'}
%
% caseconvert({'This Is A Test.' 'Matlab'},{'sentence' 'upper'})
% returns {'This is a test.' 'MATLAB'}
%
% USABLE TYPES:
%
% |-------------|------------------|------------------|
% | TYPE | SAMPLE INPUT | SAMPLE OUTPUT |
% |-------------|------------------|------------------|
% | upper | upper Case | UPPER CASE |
% | lower | Lower Case | lower case |
% | title | title case | Title Case |
% | sentence | sentence case | Sentence case |
% | toggle | Toggle Case | tOGGLE cASE |
% | randomized | randomized case | RaNDoMizEd cASe |
% |-------------|------------------|------------------|
%
% REMARKS:
%
% An initial test is recommended, especially when using non-standard or
% non-English characters, or characters with numbers or punctuations.
%
% VERSION DATE: 2005.06.10
% MATLAB VERSION: 7.0.1.24704 (R14) Service Pack 1
%
% See also LOWER, UPPER.
%{
REVISION HISTORY:
2005.06.10: Made a few minor changes as suggested by M-Lint.
2005.06.05: Vectorized two 'for' loops, and made minor comment updates.
2004.12.28: Added support for char and cell arrays.
2004.11.26: Changed to use 'switch' and 'case' instead of 'if'.
2004.11.16: Fixed bug so "Jane's" in 'title' case changes to "Jane's"
instead of "Jane'S".
2004.11.15: Original version.
KEYWORDS:
case, case convert, case conversion, lower, upper,
title case, proper case, sentence case, toggle case, randomized case,
random case
%}
%**************************************************************************
if isempty(data) || isnumeric(data)
return
elseif ischar(data)
data=cellstr(data);
data_type='char';
elseif iscell(data)
data_type='cell';
end
casetype=cellstr(casetype);
for i=1:numel(data)
data_i=caseconvert_do(cell2mat(data(i)),...
cell2mat(selectcur(casetype,i)));
if ischar(data_i)
data(i)=cellstr(data_i);
end
end
if strcmp(data_type,'char')
data=cell2mat(data);
end
%**************************************************************************
function[item]=selectcur(item,i)
if numel(item)==1
return
else
item=item(i);
end
%**************************************************************************
function[data_full]=caseconvert_do(data_full,casetype)
if isempty(data_full) || ~ischar(data_full)
return
end
%----------------------------------------------------------------------
data=data_full;
data_prefix=[];
for count=1:numel(data_full)
if isstrprop(data(1),'alphanum')==1
break
else
data_prefix=[data_prefix,data(1)];
data(1)=[];
end
end
%----------------------------------------------------------------------
data_num=numel(data);
%**************************************************************************
switch casetype
%----------------------------------------------------------------------
case 'upper'
data=upper(data);
%----------------------------------------------------------------------
case 'lower'
data=lower(data);
%----------------------------------------------------------------------
case 'title'
data(1)=upper(data(1));
for count=2:data_num
if isstrprop(data(count-1),'alphanum')==1
data(count)=lower(data(count));
else
data(count)=upper(data(count));
end
if (count>=3 && count<=data_num-1 && ...
data(count-2)~=' ' && data(count-1)=='''' && ...
data(count+1)==' ') ...
|| (count>=3 && ...
data(count-2)~=' ' && data(count-1)=='''')
data(count)=lower(data(count));
end
end
%----------------------------------------------------------------------
case 'sentence'
data(1)=upper(data(1));
data(2:end)=lower(data(2:end));
%----------------------------------------------------------------------
case 'toggle'
data_lower=find(isstrprop(data,'upper'));
data_upper=find(isstrprop(data,'lower'));
data(data_lower)=lower(data(data_lower));
data(data_upper)=upper(data(data_upper));
%----------------------------------------------------------------------
case 'randomized'
data_rand=rand(1,data_num);
data_lower=find(data_rand<=.5);
data_upper=find(data_rand>.5);
data(data_lower)=lower(data(data_lower));
data(data_upper)=upper(data(data_upper));
%----------------------------------------------------------------------
end
data_full=[data_prefix,data];
%**************************************************************************