Skip to content

Latest commit

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

CppString

STL header-only and generic string class/lib which is inspired by Java and C# string classes

Targets

  • simplify std::string usage
  • STL only
  • header only
  • fully compatiblity to std::string
  • generic methods to avoid many method overloadings
  • inheritable
  • iterateable
  • immutable

Errors/Bugs

  • nothing known

Usage

#include"String.h"using String = str::String; // for convincenceintmain() {
String s = "Hello|World"; // can be used like std::string
std::string std_str = s; // vec includes vec[0] = "Hello", vec[1] = "World";
std::vector<std::string> vec = s.split("|");
s = s.toUpper(); // "HELLO|WORLD"
s = s.addHead(" !").addTail("? ");
s = s.trim(); // "!HELLO|WORLD?"
s.count("l"); // --> 3;
std::vector<int> pos = s.find("l"); // 3, 4, 10
s.findFirst("l"); // 3
s = s.erase("ll"); // "!HEO|WORLD?"
s = s.replaceFirst("!", "?") // "?HEO|WORLD?"
s.isEmptyOrWhiteSpace(); // false
s.swap(std_str); // s == "Hello|World"
s.print(); return0;
}

Reference

Notice

Many operators and methods of String are templates. Each argument of const T<> can be used with the types char, const char*, std::string or String.

Private Members

NameType
m_strstd::string

Constructors

NameDescription
String()Initialize String with empty m_str
String(const char*)Loads const char* into m_str
String(std::string)Loads std::string into m_str
String(char)Loads char into m_str
String(int)Casts and loads int into m_str
String(long)Casts and loads long into m_str
String(float)Casts and loads float into m_str
String(double)Casts and loads double into m_str
String(bool)Initialize String with m_str equal "true" or "false"

Destructros

Name
~String()Desturcts String object

Operators

OperatorReturn ValueDescription
std::string() conststd::stringWrites m_str into std::string
=voidSaves const char*, std::string or String into internal std::string
==boolCheck if internal std::string is equal with const char*, std::string or String
!=boolCheck if internal std::string is not equal with const char*, std::string or String
+=String&Concatenates const char*, std::string or String with internal std::string
+StringConcatenates String with const char*, std::string or String
*StringMultiplies String n-times
*=String&Multiplies String n-times
/std::vectorDivides String in n pieces and saves them in vector container
[]char&Get char at the nth position
<<std::ostream&Prints a String to stdout in std::cout
>>std::istream&Writes into String from stdin with help of std::cin

Iterators

Notice: all iterators are renamed iterators from std::string

IteratorReturn Value
begin()iterator
end()iterator
cbegin()const_iterator
cend()const_iterator
rbegin()reverse_iterator
rend()reverse_iterator
crbegin()const_reverse_iterator
crend()const_reverse_iterator

Methods

Private

NameReturn ValueDescription
getStdStr(char)std::stringReturns std::string from argument
getStdStr(const char*)std::stringReturns std::string from argument
getStdStr(std::string)std::stringReturns std::string from argument
getStdStr(String)std::stringReturns std::string from argument

Public

NameReturn ValueDescription
addHead(const T<>)StringAdd substring on head of String
addTail(const T<>)StringAdd substring on tail of String
charAt(const int)charGet character at specific position
compare(const T<>)boolCompares String with String or std::string
concat(const T<>)StringConcats String with const char*, std::string or String
contains(const T<>)boolChecks if String contains a specific substring
copyTo(String )voidSaves reference of internal std::string to other internal std::string of String
copyTo(std::string&)voidSaves reference of internal std::string to other std::string
count(const T<>)intReturns number of occurences of const char*, std::string or String argument in String
endsWith(const T<>)boolChecks if a String ends with a specific substring
equals(const T<>)boolAlias to compare(const T<>)
erase(const T<>)StringErases all occurences of substring
eraseFirst(const T<>)StringErases first occurence of substring
eraseHead(int)StringErases head of String
eraseLast(const T<>)StringErases last occurence of substring
eraseTail(int)StringErases tail of String
fillHead(const int, const char)StringFill head of String with number of characters
fillTail(const int, const char)StringFill tail of String with number of characters
find(const T<>)std::vectorSearch all occurences of substring and saves position of them in vector container
findAll(const T<>)std::vectorAlias of find(const T<>)
findFirst(const T<>)intReturns position of first occurence of substring
findLast(const T<>)intReturns position of last occurence of substring
format(const T<>1, const T<>2)StringReplaces {%} with given arguments
indexOf(const T<>)intAlias of findFirst(const T<>)
isEmpty()boolChecks if String is empty string ""
isEmptyOrWhiteSpace()boolChecks if String is empty string or string with WhiteSpaces only
lastIndexOf(const T<>)intAlias of findLast(const T<>)
length()intGet length of String
padLeft(const int, const char)StringAlias for fillHead(const int, const char)
padRight(const int, const char)StringAlias for fillTail(const int, const char)
print()voidPrints String to stdout
print(const T<>, const T<> = "")voidPrints const char*, std::string or String to stdout
print(const T<> = "", const T<>, const T<>...)voidPrints n const char*, std::string or String to stdout with specific separator
replace(const T<>)StringReplaces all occurences of substring
replaceFirst(const T<>)StringReplaces first occurence of substring
replaceHead(int, const T<>)StringReplaces head of String with new substring
replaceLast(const T<>)StringReplaces last occurence of substring
replaceTail(int, const T<>)StringReplaces tail of String with new substring
reverse()StringReverse String
size()intAlias of length()
split(const T<>)std::vectorSplits string with help of delimiter (char or string) into substrings and saves them in a container
startsWith(const T<>)boolChecks if a String starts with a specific substring
swap(std::string&)voidSwaps String and std::string
swap(String &s)voidSwaps String and String
toBool()boolCasts String to bool (if "0", "false", "" then "false" else "true")
toCharArr()std::vectorReturns container with characters
toCStr()const char*Returns const char* from String
toStdStr()StringReturns internal std::string of String
toDouble()doubleReturns double if String is numeric
toFloat()floatReturns float if String is numeric
toInt()intReturns Integer if String is numeric
toLong()longReturns Long if String is numeric
toLower()StringFormats String to lower case
toUpper()StringFormats String to upper case
trim()StringTrims WhiteSpaces on
trimHead()StringTrims WhiteSpaces at head of String
trimTail()StringTrims WhiteSpaces at tail of String

About

STL header-only string class/lib which is orientated to Java and C# string classes

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages