sources/mystring.cpp

changeset 137
485cb6d6b98c
parent 129
a556ce001e26
child 141
d9073c13dc98
child 145
d0aedc9be448
equal deleted inserted replaced
132:8a4690db252e 137:485cb6d6b98c
68 // 68 //
69 String String::strip (const List<char>& unwanted) const 69 String String::strip (const List<char>& unwanted) const
70 { 70 {
71 String result (m_string); 71 String result (m_string);
72 72
73 for (int i = 0; i < unwanted.size(); ++i) 73 for (String c : unwanted)
74 { 74 for (int pos = 0; (pos = result.find (c)) != -1;)
75 String c = unwanted[i]; 75 result.remove_at (pos--);
76
77 for (int pos = 0; (pos = result.find (c)) != -1;)
78 result.remove_at (pos--);
79 }
80 76
81 return result; 77 return result;
82 } 78 }
83 79
84 // ------------------------------------------------------------------------------------------------- 80 // -------------------------------------------------------------------------------------------------
85 // 81 //
86 String String::to_uppercase() const 82 String String::to_uppercase() const
87 { 83 {
88 String result (m_string); 84 String result (m_string);
89 85
90 for (int i = 0; i < result.length(); ++i) 86 for (char &ch : result)
91 { 87 {
92 if (islower (result[i])) 88 if (islower(ch))
93 result[i] -= 'a' - 'A'; 89 ch -= 'a' - 'A';
94 } 90 }
95 91
96 return result; 92 return result;
97 } 93 }
98 94
100 // 96 //
101 String String::to_lowercase() const 97 String String::to_lowercase() const
102 { 98 {
103 String result (m_string); 99 String result (m_string);
104 100
105 for (int i = 0; i < result.length(); ++i) 101 for (char &ch : result)
106 { 102 {
107 if (isupper (result[i])) 103 if (isupper(ch))
108 result[i] += 'a' - 'A'; 104 ch += 'a' - 'A';
109 } 105 }
110 106
111 return result; 107 return result;
112 } 108 }
113 109
160 // 156 //
161 int String::count (char needle) const 157 int String::count (char needle) const
162 { 158 {
163 int result = 0; 159 int result = 0;
164 160
165 for (int i = 0; i < length(); ++i) 161 for (char ch : *this)
166 { 162 {
167 if (m_string[i] == needle) 163 if (ch == needle)
168 result++; 164 result++;
169 } 165 }
170 166
171 return result; 167 return result;
172 } 168 }
203 // 199 //
204 int String::word_position (int n) const 200 int String::word_position (int n) const
205 { 201 {
206 int count = 0; 202 int count = 0;
207 203
208 for (int i = 0; i < length(); ++i) 204 for (char ch : *this)
209 { 205 {
210 if (not isspace (m_string[i]) or ++count < n) 206 if (not isspace(ch) or ++count < n)
211 continue; 207 continue;
212
213 return i;
214 } 208 }
215 209
216 return -1; 210 return -1;
217 } 211 }
218 212
377 // 371 //
378 String StringList::join (const String& delim) 372 String StringList::join (const String& delim)
379 { 373 {
380 String result; 374 String result;
381 375
382 for (int i = 0; i < size(); ++i) 376 for (const String &item : container())
383 { 377 {
384 if (result.is_empty() == false) 378 if (result.is_empty() == false)
385 result += delim; 379 result += delim;
386 380
387 result += container()[i]; 381 result += item;
388 } 382 }
389 383
390 return result; 384 return result;
391 } 385 }
392 386

mercurial