Skip to content

madureira/String

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

String

A lightweight, dependency-free and header only C++ custom string.

language

Install

Just add the String.h to your project or include path.

How to use

#include "String.h"

String text("Hello, world!");

Features

Methods Return Description
String() Instance of String (Constructor) Create an empty new instance of the String class.
String(String& string) Instance of String (Constructor) Create a new instance of the String class based on an existent String reference.
String(const char* cString) Instance of String (Constructor) Create a new instance of the String class based on an existent C string pointer.
Size() int Returns the String length.
c_str() const char* Returns the C string pointer.
Equals(String& other) bool Compare the current String with another and return true if they are the same or false, otherwise.
Equals(const char* other) bool Compare the current String with a C string and return true if they are the same or false, otherwise.
Index(char c) int Returns the position of the first occurrence of a specified value in a string or -1 if the value to search for never occurs.
Concat(const String& string) String& Join the current String with another String value and returns its reference.
Concat(const char* string) String& Join the current String with another C string value and returns its reference.
ToUpperCase() String& Converts a string to uppercase letters and returns its reference.
ToLowerCase() String& Converts a string to lowercase letters and returns its reference.
Find(String& string) int Searches a string for a specified value, and returns the position of the match.
Find(const char* cString) int Searches a string for a specified value, and returns the position of the match.
Replace(String& target, String& replacement) String& Searches a string for a specified value, and replace it by another given string and returns its reference.
Replace(String& target, const char* replacement) String& Searches a string for a specified value, and replace it by another given string and returns its reference.
Replace(const char* target, String& replacement) String& Searches a string for a specified value, and replace it by another given string and returns its reference.
Replace(const char* target, const char* replacement) String& Searches a string for a specified value, and replace it by another given string and returns its reference.

Examples

Creating a empty String:

String emptyString;

Creating a new String:

String foo("foo");

String bar = "bar";

String assignment by reference:

String foo("foo");

String bar("bar");

foo = bar;

printf("%s", foo.c_str()); // bar

Copy existent String value by constructor:

String text("foo");

String anotherText(text); // foo

Get the length of a String:

String text("foo");

text.Size(); // 3

Print the String:

String text("foo");

std::cout << text.c_str() << std::endl; // foo

printf("%s", text.c_str()); // foo

Comparing Strings:

String foo("foo");
String bar("bar");

bool isEquals = foo.Equals(bar); // false

isEquals = foo.Equals("bar"); // false

isEquals = foo.Equals("foo"); // true

isEquals = (foo == bar); // false

isEquals = (foo == "bar"); // false

isEquals = (bar == "bar"); // true

isEquals = (bar != foo); // true

Retrieving the position of the first occurrence char:

String text("Hello, world!");

int index = text.Index(','); // 5

int notfound = text.Index('$'); // -1

Retrieving char by index:

String text("Hello, world!");

char c = text[7]; // w

Concatenate Strings:

String foo("foo");
String bar(" bar");

foo.Concat(bar).Concat("!!!");

printf("%s", foo.c_str()); // foo bar!!!

String hello("hello, ");
String world("world!!!");

hello += world;

printf("%s", hello.c_str()); // hello, world!!!

String amazing("amazing ");
String text("text");

String message = amazing + text;

printf("%s", message.c_str()); // amazing text

Converts a String to uppercase letters:

String text("foo");

text.ToUpperCase(); // FOO

Converts a String to lowercase letters:

String text("BAR");

text.ToLowerCase(); // bar

Finds the position of the first occurrence of a value in a String:

String text("Some amazing text");

int index = text.Find("amazing"); // 5

String another("Some");

int position = text.Find(another); // 0

int notfound = text.Find("xyz"); // -1

Replace the position of the first occurrence of value by another one:

String text("Some amazing text");

text.Replace("amazing", "silly"); // Some silly text

About

🔌 A lightweight, dependency-free and header only C++ custom string.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages