From d2cfd39f806fc197ce49b27c8df2cb265b6ddeb1 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 29 May 2008 03:46:42 +0000 Subject: [PATCH] added amb1694 - FindCharInString --HG-- branch : sourcemod-1.0.x extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/branches/sourcemod-1.0.x%402206 --- plugins/include/string.inc | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/plugins/include/string.inc b/plugins/include/string.inc index bfd1039b..9ff75eed 100644 --- a/plugins/include/string.inc +++ b/plugins/include/string.inc @@ -421,6 +421,46 @@ stock CharToLower(chr) return chr; } +/** + * Finds the first occurrence of a character in a string. + * + * @param str String. + * @param c Character to search for. + * @param reverse False (default) to search forward, true to search + * backward. + * @return The index of the first occurrence of the character + * in the string, or -1 if the character was not found. + */ +stock FindCharInString(const String:str[], c, bool:reverse = false) +{ + new i, len + + len = strlen(str); + + if (!reverse) + { + for (i = 0; i < len; i++) + { + if (str[i] == c) + { + return i; + } + } + } + else + { + for (i = len - 1; i >= 0; i--) + { + if (str[i] == c) + { + return i; + } + } + } + + return -1; +} + /** * Concatenates one string onto another. *