diff options
author | Chaosus <chaosus89@gmail.com> | 2019-01-18 11:29:28 +0300 |
---|---|---|
committer | Chaosus <chaosus89@gmail.com> | 2019-07-23 18:55:54 +0300 |
commit | 080c0bb7fea824f231e8972fe1e7f82290a2f453 (patch) | |
tree | 70c99fe65ad4e7a4d294206e1c65345e6f30116b /modules/mono/glue | |
parent | c7a427241e7f4dd6ab9ac89f51d3bb9f6632008b (diff) |
Added count method to String
Diffstat (limited to 'modules/mono/glue')
-rw-r--r-- | modules/mono/glue/Managed/Files/StringExtensions.cs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/modules/mono/glue/Managed/Files/StringExtensions.cs b/modules/mono/glue/Managed/Files/StringExtensions.cs index b43034fbb5..6045c83e95 100644 --- a/modules/mono/glue/Managed/Files/StringExtensions.cs +++ b/modules/mono/glue/Managed/Files/StringExtensions.cs @@ -98,6 +98,66 @@ namespace Godot } // <summary> + // Return the amount of substrings in string. + // </summary> + public static int Count(this string instance, string what, bool caseSensitive = true, int from = 0, int to = 0) + { + if (what.Length == 0) + { + return 0; + } + + int len = instance.Length; + int slen = what.Length; + + if (len < slen) + { + return 0; + } + + string str; + + if (from >= 0 && to >= 0) + { + if (to == 0) + { + to = len; + } + else if (from >= to) + { + return 0; + } + if (from == 0 && to == len) + { + str = instance; + } + else + { + str = instance.Substring(from, to - from); + } + } + else + { + return 0; + } + + int c = 0; + int idx; + + do + { + idx = str.IndexOf(what, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase); + if (idx != -1) + { + str = str.Substring(idx + slen); + ++c; + } + } while (idx != -1); + + return c; + } + + // <summary> // Return a copy of the string with special characters escaped using the C language standard. // </summary> public static string CEscape(this string instance) |