Skip to content

Commit 251d7f4

Browse files
beutlichm-kesslertobolar
authored
Add function Strings.contains (#4493)
* Add function Strings.contains * Return true for empty search strings of Strings.contains In accordance with python, java, etc. * Add tests for Strings.contains * Fix documentation * Fix documentation Co-authored-by: tobolar <[email protected]> * Update documentation Co-authored-by: tobolar <[email protected]> --------- Co-authored-by: mkr7 <[email protected]> Co-authored-by: tobolar <[email protected]>
1 parent 1166ac5 commit 251d7f4

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Modelica/Utilities/Strings.mo

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,52 @@ isEmpty(\"a\"); // returns false
185185
</html>"));
186186
end isEmpty;
187187

188+
function contains "Check if a string contains a substring"
189+
190+
extends Modelica.Icons.Function;
191+
192+
input String string "String to check";
193+
input String searchString "Substring to look for";
194+
input Boolean caseSensitive=true
195+
"= false, if lower and upper case are ignored for the search";
196+
output Boolean result "= true, if searchString can be found anywhere in string";
197+
198+
algorithm
199+
if length(searchString) > 0 then
200+
result := find(string, searchString, 1, caseSensitive) > 0;
201+
else
202+
result := true;
203+
end if;
204+
205+
annotation (Documentation(info="<html>
206+
<h4>Syntax</h4>
207+
<blockquote><pre>
208+
result = Strings.<strong>contains</strong>(string, searchString);
209+
result = Strings.<strong>contains</strong>(string, searchString, caseSensitive);
210+
</pre></blockquote>
211+
212+
<h4>Description</h4>
213+
<p>
214+
Returns true if \"searchString\" is a substring of \"string\". Otherwise, false is returned.
215+
The optional argument \"caseSensitive\" controls if substrings match, which differ in case only.
216+
</p>
217+
<p>
218+
Empty strings are treated as substring of all strings. Therefore, the result is always true if \"searchString\" is empty.
219+
</p>
220+
221+
<h4>Example</h4>
222+
<blockquote><pre>
223+
import Modelica.Utilities.Strings.contains;
224+
225+
contains(\"foobar\", \"OO\"); // returns false
226+
contains(\"foobar\", \"OO\", false); // returns true
227+
contains(\"foo\", \"bar\"); // returns false
228+
contains(\"\", \"\"); // returns true
229+
contains(\"foo\", \"\"); // returns true
230+
</pre></blockquote>
231+
</html>"));
232+
end contains;
233+
188234
function count "Count the number of non-overlapping occurrences of a string"
189235
extends Modelica.Icons.Function;
190236
input String string "String that is analyzed";

ModelicaTest/Utilities.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ extends Modelica.Icons.ExamplesPackage;
215215
assert(hash1 <> Strings.hashString("this is a tes1"), "Strings.hashString 1 failed");
216216
assert(hash1 <> Strings.hashString("this is a tes2"), "Strings.hashString 1 failed");
217217

218+
// Strings.contains
219+
assert(Strings.contains("", ""), "Strings.contains 1 failed (empty strings)");
220+
assert(Strings.contains("foo bar", "o"), "Strings.contains 2 failed (multiple occurences)");
221+
assert(Strings.contains("foo bar", "o ba"), "Strings.contains 3 failed (occurrence at middle)");
222+
assert(Strings.contains("foo bar", "fo"), "Strings.contains 4 failed (occurrence at start)");
223+
assert(Strings.contains("foo bar", "ar"), "Strings.contains 5 failed (occurrence at end)");
224+
assert(not Strings.contains("foo bar", "x"), "Strings.contains 6 failed (no occurrence)");
225+
assert(Strings.contains("foo", ""), "Strings.contains 7 failed (empty search string)");
226+
assert(not Strings.contains("foo bar", "O"), "Strings.contains 8 failed (case sensitive)");
227+
assert(Strings.contains("foo bar", "O", false), "Strings.contains 9 failed (case insensitive)");
228+
218229
ok := true;
219230
end Strings;
220231

0 commit comments

Comments
 (0)