From dee1c92532f7939c772f8d7345f249ebea55ff79 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 22 Jan 2023 18:08:50 -0500 Subject: [PATCH] string contains functions as well --- include/blt/std/string.h | 22 ++++++++++++++++++++++ src/tests/main.cpp | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/blt/std/string.h b/include/blt/std/string.h index 1056438..f4fb470 100644 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -7,6 +7,7 @@ #ifndef BLT_STRING_H #define BLT_STRING_H +#include #include #include #include @@ -38,6 +39,27 @@ namespace blt::String { } return true; } + + static inline bool contains(const std::string& string, const std::string& search){ + if (search.length() > string.length()) + return false; + auto chars = string.c_str(); + auto search_chars = search.c_str(); + for (int i = 0; i < string.length(); i++){ + if (chars[i] == search_chars[0]) { + bool correct = true; + for (int j = 0; j < search.length(); j++) { + if (chars[i + j] != search_chars[j]) { + correct = false; + break; + } + } + if (correct) + return true; + } + } + return false; + } /** * Converts the string into lower case * @param s string to lower case diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 1002e5e..912419b 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -6,5 +6,5 @@ int main() { binaryTreeTest(); std::string hello = "superSexyMax"; - std::cout << "String starts with: " << blt::String::ends_with(hello, "Max") << "\n"; + std::cout << "String starts with: " << blt::String::contains(hello, "superSexyMaxE") << "\n"; } \ No newline at end of file