Published Aug 4, 2021Updated Aug 13, 2022
Contribute to Docs
Strings are objects that represent sequences of characters. In C++, the two ways to create strings are with the
string class or with C-style character strings.
string class provides support for strings in C++.
std::string welcome = "Hi";std::string user_name = "@sonny";std::string message = "Good nite! 😇";
C-Style Character Strings
The C-style character string originated from the C language and continues to be supported within C++.
In C, the string is actually an array of characters, followed by a
char message = "Howdy";
So here’s the memory presentation:
Character | 'H' 'o' 'w' 'd' 'y' '\0'Index | 0 1 2 3 4 5Address | 23451 23452 23453 23454 23455 23456
- Returns the index of the first occurrence of the specified string or character.
- Returns the length of the string it is called on.
- Returns a string with a portion replaced by another string.
- Returns the size of the string it is called on.
- Returns a portion of a string specified by a starting position and length.
- Compares two strings and returns an integer value.
- Copies one string into another and returns the newly copied string.
- Breaks a C-style string into a series of tokens using a list of delimiters.
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification