52 |
54 |
53 // ------------------------------------------------------------------------------------------------- |
55 // ------------------------------------------------------------------------------------------------- |
54 // |
56 // |
55 String String::strip (const List<char>& unwanted) |
57 String String::strip (const List<char>& unwanted) |
56 { |
58 { |
57 String copy (m_string); |
59 String result (m_string); |
58 |
60 |
59 for (char c : unwanted) |
61 for (int i = 0; i < unwanted.size(); ++i) |
60 { |
62 { |
61 for (int pos = 0; (pos = copy.find (String (c))) != -1;) |
63 String c = unwanted[i]; |
62 copy.remove_at (pos--); |
64 |
63 } |
65 for (int pos = 0; (pos = result.find (c)) != -1;) |
64 |
66 result.remove_at (pos--); |
65 return copy; |
67 } |
|
68 |
|
69 return result; |
66 } |
70 } |
67 |
71 |
68 // ------------------------------------------------------------------------------------------------- |
72 // ------------------------------------------------------------------------------------------------- |
69 // |
73 // |
70 String String::to_uppercase() const |
74 String String::to_uppercase() const |
71 { |
75 { |
72 String newstr (m_string); |
76 String result (m_string); |
73 |
77 |
74 for (char& c : newstr) |
78 for (int i = 0; i < result.length(); ++i) |
75 { |
79 { |
76 if (c >= 'a' and c <= 'z') |
80 if (islower (result[i])) |
77 c -= 'a' - 'A'; |
81 result[i] -= 'a' - 'A'; |
78 } |
82 } |
79 |
83 |
80 return newstr; |
84 return result; |
81 } |
85 } |
82 |
86 |
83 // ------------------------------------------------------------------------------------------------- |
87 // ------------------------------------------------------------------------------------------------- |
84 // |
88 // |
85 String String::to_lowercase() const |
89 String String::to_lowercase() const |
86 { |
90 { |
87 String newstr (m_string); |
91 String result (m_string); |
88 |
92 |
89 for (char& c : newstr) |
93 for (int i = 0; i < result.length(); ++i) |
90 { |
94 { |
91 if (c >= 'A' and c <= 'Z') |
95 if (isupper (result[i])) |
92 c += 'a' - 'A'; |
96 result[i] += 'a' - 'A'; |
93 } |
97 } |
94 |
98 |
95 return newstr; |
99 return result; |
96 } |
100 } |
97 |
101 |
98 // ------------------------------------------------------------------------------------------------- |
102 // ------------------------------------------------------------------------------------------------- |
99 // |
103 // |
100 StringList String::split (char del) const |
104 StringList String::split (char del) const |
142 |
146 |
143 // ------------------------------------------------------------------------------------------------- |
147 // ------------------------------------------------------------------------------------------------- |
144 // |
148 // |
145 int String::count (char needle) const |
149 int String::count (char needle) const |
146 { |
150 { |
147 int needles = 0; |
151 int result = 0; |
148 |
152 |
149 for (const char & c : m_string) |
153 for (int i = 0; i < length(); ++i) |
150 if (c == needle) |
154 { |
151 needles++; |
155 if (m_string[i] == needle) |
152 |
156 result++; |
153 return needles; |
157 } |
|
158 |
|
159 return result; |
154 } |
160 } |
155 |
161 |
156 // ------------------------------------------------------------------------------------------------- |
162 // ------------------------------------------------------------------------------------------------- |
157 // |
163 // |
158 String String::mid (long a, long b) const |
164 String String::mid (long a, long b) const |
159 { |
165 { |
160 if (b == -1 or b > length()) |
166 if (b == -1 or b > length()) |
161 b = length(); |
167 b = length(); |
162 |
168 |
163 if (b == a) |
169 if (b <= a) |
164 return ""; |
170 return ""; |
165 |
|
166 if (b < a) |
|
167 swap (a, b); |
|
168 |
171 |
169 if (a == 0 and b == length()) |
172 if (a == 0 and b == length()) |
170 return *this; |
173 return *this; |
171 |
174 |
172 char* newstr = new char[b - a + 1]; |
175 char* newstr = new char[b - a + 1]; |
177 return other; |
180 return other; |
178 } |
181 } |
179 |
182 |
180 // ------------------------------------------------------------------------------------------------- |
183 // ------------------------------------------------------------------------------------------------- |
181 // |
184 // |
|
185 String String::right(int length) const |
|
186 { |
|
187 if (length >= this->length()) |
|
188 return *this; |
|
189 else |
|
190 return String(chars() + this->length() - length); |
|
191 } |
|
192 |
|
193 // ------------------------------------------------------------------------------------------------- |
|
194 // |
182 int String::word_position (int n) const |
195 int String::word_position (int n) const |
183 { |
196 { |
184 int count = 0; |
197 int count = 0; |
185 |
198 |
186 for (int i = 0; i < length(); ++i) |
199 for (int i = 0; i < length(); ++i) |