diff --git a/Modelica/Utilities/Strings.mo b/Modelica/Utilities/Strings.mo index 9c184fee84..bc559092dd 100644 --- a/Modelica/Utilities/Strings.mo +++ b/Modelica/Utilities/Strings.mo @@ -185,6 +185,52 @@ isEmpty(\"a\"); // returns false ")); end isEmpty; + function contains "Check if a string contains a substring" + + extends Modelica.Icons.Function; + + input String string "String to check"; + input String searchString "Substring to look for"; + input Boolean caseSensitive=true + "= false, if lower and upper case are ignored for the search"; + output Boolean result "= true, if searchString can be found anywhere in string"; + + algorithm + if length(searchString) > 0 then + result := find(string, searchString, 1, caseSensitive) > 0; + else + result := true; + end if; + + annotation (Documentation(info=" +

Syntax

+
+result = Strings.contains(string, searchString);
+result = Strings.contains(string, searchString, caseSensitive);
+
+ +

Description

+

+Returns true if \"searchString\" is a substring of \"string\". Otherwise, false is returned. +The optional argument \"caseSensitive\" controls if substrings match, which differ in case only. +

+

+Empty strings are treated as substring of all strings. Therefore, the result is always true if \"searchString\" is empty. +

+ +

Example

+
+import Modelica.Utilities.Strings.contains;
+
+contains(\"foobar\", \"OO\");         // returns false
+contains(\"foobar\", \"OO\", false);  // returns true
+contains(\"foo\", \"bar\");           // returns false
+contains(\"\", \"\");                 // returns true
+contains(\"foo\", \"\");              // returns true
+
+")); + end contains; + function count "Count the number of non-overlapping occurrences of a string" extends Modelica.Icons.Function; input String string "String that is analyzed"; diff --git a/ModelicaTest/Utilities.mo b/ModelicaTest/Utilities.mo index 9d1bb972ec..88d104cdf9 100644 --- a/ModelicaTest/Utilities.mo +++ b/ModelicaTest/Utilities.mo @@ -215,6 +215,17 @@ extends Modelica.Icons.ExamplesPackage; assert(hash1 <> Strings.hashString("this is a tes1"), "Strings.hashString 1 failed"); assert(hash1 <> Strings.hashString("this is a tes2"), "Strings.hashString 1 failed"); + // Strings.contains + assert(Strings.contains("", ""), "Strings.contains 1 failed (empty strings)"); + assert(Strings.contains("foo bar", "o"), "Strings.contains 2 failed (multiple occurences)"); + assert(Strings.contains("foo bar", "o ba"), "Strings.contains 3 failed (occurrence at middle)"); + assert(Strings.contains("foo bar", "fo"), "Strings.contains 4 failed (occurrence at start)"); + assert(Strings.contains("foo bar", "ar"), "Strings.contains 5 failed (occurrence at end)"); + assert(not Strings.contains("foo bar", "x"), "Strings.contains 6 failed (no occurrence)"); + assert(Strings.contains("foo", ""), "Strings.contains 7 failed (empty search string)"); + assert(not Strings.contains("foo bar", "O"), "Strings.contains 8 failed (case sensitive)"); + assert(Strings.contains("foo bar", "O", false), "Strings.contains 9 failed (case insensitive)"); + ok := true; end Strings;