/StringUtils

This library implements many commonly used but not natively supported function in std::string, like Split, Join, Trim, Compact, ReplaceAll, ToLower, ToUpper, Repeat, ReadFile, WriteFile, etc

Primary LanguageC++The UnlicenseUnlicense

StringUtils

This library implements many commonly used but not natively supported function in std::string, like Split, Join, Trim, Compact, ReplaceAll, ToLower, ToUpper, Repeat, ReadFile, WriteFile, etc.

Test

make test

Passing: Split(s, delim, false)
Passing: Split(s, delim, true)
Passing: Join(res, delim, false)
Passing: Join(res, delim, true)
Passing: Compact(res)
Passing: Trim(" hi ")
Passing: Repeat("-", 5)
Passing: ReplaceAll("monday morning", "mo", "kk")
Passing: ToUpper("abcd")
Passing: ToLower("ABCD")
Passing: ReadFile("test_readfile.txt")
Passing: WriteFile("test_writefile.txt", "abcde")
Result: 12/12 Completed.

Usage

see example: test.cpp

see source code: string-utils.h

see demo: demo

Split

vector<string> v = Split("one,two,three", ","); // v: ["one", "two", "three"]

Join

vector<string> v;
v.push_back("1");
v.push_back("2");
v.push_back("3");
string s = Join(v, ":"); // s: "1:2:3"

Trim

string s = Trim(" hi "); // v: "hi"

Repeat

string s = Repeat("#", 5); // v: "#####"

ReplaceAll

vector<string> v = ReplaceAll("1-2-3-4", "-", "#"); // v: "1#2#3#4"

Compact

remove empty string

vector<string> v;
v.push_back("1");
v.push_back("");
v.push_back("2");
v.push_back("3");
v.push_back("");
// v: ["1", "", "2", "3", ""]
v = Compact(v);
// v: ["1", "2", "3"]

ToLower

string s = ToLower("ABCD"); // v: "abcd"

ToUpper

string s = ToUpper("abcd"); // v: "ABCD"

ReadFile

read file to string

string content = ReadFile(...file path...);

WriteFile

write string to file

string content = "...";
WriteFile(...file path..., content);

Demo

build and run: make demo

see demo: demo

Contributing

We love contributions! If you'd like to contribute please submit a pull request.

License

License.md