The aim of this project is not to make a very complicated library. I am trying to make it as simple as possible. Simple but powerful. Considering underscore.string is a very awesome javascript library for working with strings, stringplus tries to do things similar.
To use stringplus you have to include stringplus.h wherever you need it
#include "stringplus.h"
and then all the functionalities are available in the stringplus namespace.
If you don't want to use the namespace every time:
using namespace stringplus;
Then you have to compile stringplus.cc with your program. For example:
g++ -std=c++11 main.cc stringplus.cc
Replaces lower case alphabetical chars with upper case.
stringplus::to_upper("foo bar");
// => "FOO BAR"
Replaces upper case alphabetical chars with lower case.
stringplus::to_lower("FOO BAR");
// => "foo bar"
Returns true if all chars in the string are alphabetical.
stringplus::is_alpha("abcd");
// => true
stringplus::is_alpha("Abn4");
// => false
Returns true if all chars in the string are digits.
stringplus::is_digit("1234");
// => true
stringplus::is_digit("34n4");
// => false
Returns true if all chars in the string are digits or an uppercase or lowercase letter.
stringplus::is_alpha_digit("1234");
// => true
stringplus::is_alpha_digit("34n4");
// => true
stringplus::is_alpha_digit("34n[");
// => false
Returns true if all alpha chars in the string are lowercases.
stringplus::is_lower("1234");
// => true
stringplus::is_lower("34n4");
// => true
stringplus::is_lower("34M");
// => false
Returns true if all alpha chars in the string are uppercases.
stringplus::is_upper("1234");
// => true
stringplus::is_upper("34N4");
// => true
stringplus::is_upper("34n");
// => false
Converts first letter of the string to uppercase. If true is passed as second argument the rest of the string will be converted to lower case.
stringplus::capitalize("abcd");
// => "Abcd"
stringplus::capitalize("aBCD");
// => "ABCD"
stringplus::capitalize("aBCD", true);
// => "Abcd"
Converts first letter of the string to lowercase.
stringplus::decapitalize("ABCd");
// => "aBCD"
Chops the string to a vector with the given step.
stringplus::chop("whitespace", 3);
// => ["whi", "tes", "pac", "e"]
Trim and replace multiple spaces with a single space.
stringplus::clean(" foo bar ");
// => "foo bar"
Returns a copy of the string in which all the case-based characters have had their case swapped.
stringplus::swap_case("hELLO");
// => "Hello"
Return reversed string:
stringplus::reverse("Hello");
// => "olleH"
stringplus::reverse("12345");
// => "54321"
Truncates the strings with the length greater than the given length with the given string.
stringplus::truncate("Hello world", 5);
// => "Hello..."
stringplus::truncate("Hello", 10);
// => "Hello"
stringplus::truncate("Hello world", 5, " read more");
// => "Hello read more"
Surrounds a string with another string.
stringplus::surround("foo", "bar");
// => "barfoobar"
Repeats a string count times, seperated by given string.
stringplus::repeat("foo", 3, "bar");
// => "foobarfoobarfoo"
####pad(string, int, [char = ' ', string = "left"]) => string pads the string with character until the total string length is equal to the passed length parameter. By default, pads on the left with the space char (" ").
stringplus::pad("1", 8);
// => " 1"
stringplus::pad("1", 8, "0");
// => "00000001"
stringplus::pad("1", 8, "0", "right");
// => "10000000"
stringplus::pad("1", 8, "0", "both");
// => "00001000"
left-pad a string. Alias for pad(string, int, char, "left")
stringplus::lpad("1", 8, "0");
// => "00000001"
right-pad a string. Alias for pad(string, int, char, "right")
stringplus::rpad("1", 8, "0");
// => "10000000"
left/right-pad a string. Alias for pad(string, int, char, "both")
stringplus::lrpad("1", 8, "0");
// => "00001000"
Checks whether the string begins with starts at position.
stringplus::starts_with("image.gif", "image");
// => true
stringplus::starts_with(".vimrc", "vim");
// => false
stringplus::starts_with(".vimrc", "vim", 1);
// => true
Checks whether the string ends with ends at position (default: str.length()).
stringplus::ends_with("image.gif", "gif");
// => true
stringplus::ends_with("image.old.gif", "old");
// => false
stringplus::ends_with("image.old.gif", "old", 9);
// => true
Joins strings together with given separator.
stringplus::join(" ", {"foo", "bar"});
// => "foo bar"
Returns number of occurrences of substring in a string.
stringplus::count("Hello world", "l");
// => 3