Team 2550
Technical Documentation
|
|
Strings are not as simple to use in C++ as they are in many other languages. This page provides information about how to work with strings.
A c-string is simply a null-terminated array of characters. In other words, a c-string is an array of char
variables containing an ASCII \0
character at the end.
You have actually used c-strings already. Any time you placed text in quotes, the compiler treats it as an (anonymous) c-string. However, the C standard library also included some basic functions for manipulating such strings.
You can output a c-string with cout
. The null termination allows it to determine exactly how long it is.
char str[] = "This is a test"; cout << str;
Outputs
This is a test
#include <cstring>
provides various functions that can be used to manipulate c-strings.
Function | Action | Example |
---|---|---|
strlen(string) | Returns the length of a c-string up to the first null character | char str[] = "Hello";
strlen(str);
//Result: 5
|
strcpy(destination, source) | Copies the contents of one c-string into another | #include <cstring>
using namespace std;
int main() {
char str1[] = "string to copy";
char str2[100] = {0};
strcpy(str2, str1); //copies str1 into str2
}
|
strcat(destination, source) | Merges two strings, the result is added to the end of the destination string. | char str1[] = "Hello";
char str2[100] = " world";
strcat(str1, str2);
//Result: "Hello world"
|
strcmp(string1, string2) | Compares
Returns
| char str1[10] = "hello";
char str2[10] = "test";
strcmp(str1, str2); //Result: -12
strcpy(str1, "test");
strcmp(str1, str2); //Result: 0
strcpy(str2, "no");
strcmp(str1, str2); //Result: 6
|
|
Both return |
char str[] = "hello";
strchr(str, 'l'); //Result: "llo"
strchr(str, '\n'); //Result: NULL
char path[] = "/this/is/a/file/path/to/file.txt";
strchr(path, '/'); //Result: /file.txt
|
strstr(str, search) | Searches for a substring in a string. | char str[] = "this is a test string";
strstr(str, "test"); //Result: "test string"
|
strncpy strncat strncmp | These functions are the same as their counterparts, strcpy , strcat , and strcmp . The only difference is that there is a third parameter which represents the number of characters upon which to perform the operation. For instance, strncpy will copy the first n characters from one string into another. | //Copy a part of a string into another, smaller, string.
char str1[] = "this is a test string";
char str2[10];
strncpy(str2, str1, 9);
// str2 = "this is a"
strlen(str2); //Result: 9
|
The normal streaming operators work with c-strings. You can output with cout <<
and input with cin >>
.
cin
has one major problem. It is whitespace-delimited. The following code illustrates the problem.If the user inputs something with whitespace, such as
this is my input
cin
will place the word "this" into str
and leave the other words for later. In order to avoid this problem, you have to use cin.get
. Problem solved.
The cin.get(str, 100)
line accepts up to 99 characters from the user, whitespace included, and writes it all into str
.
C-strings work, but they can get complicated. The C++ standard library (the built-in tools that come with every C++ compiler) has a string datatype that manages c-strings for you. It is much easier to use, but there are also times where it can be less efficient.
std::string
.A few of the benefits of std::string
:
You don't need functions to do basic operations with strings. The normal arithmetic operators work.
You can use the assignment =
operator to change the value of a string.
The above code is valid. This would cause problems with c-strings.
The +
operator concatinates (combines) two strings.
You can also concatinate a string with a c-string.
==
, !=
, <
, >
, <=
, and >=
all work with strings. For now, only ==
and !=
are really useful.
Again, cout << ...
and cin >>
both work with std::string
variables. However, the same input problem exists when using cin >>
(see c-string I/O). However, the solution is slightly different.
A method is a function that operates on a specific variable (such as a string). The format for calling a method is variable.method()
.
Method | Action | Example |
---|---|---|
at(index) | Accesses a character in the string (array notation). | string str = "hello";
str.at(0) //Result: h
|
c_str() | Returns an unmodifiable c-string. This can be useful for functions that take a c-string as an argument, for instance, opening files. | string str = "test";
str.c_str();
|
empty() | Checks whether the string is empty. | string str = "";
str.empty(); //Result: true
|
size() length() | Returns the length of the string. | string str = "this is a string";
str.size(); //Result: 16
|
clear() | Erases the contents of the string. | string str = "this will be erased";
str.clear(); //str = "";
|
insert(index, substring) insert(index, substring, count) insert(index, count, character) | Adds a character or substring starting at the given index in a string without erasing data. count sets the number of times a character or substring is repatedly added. | string str = "forgot in this string";
str.insert(6, " something"); //str = "forgot something in this string"
str.insert(str.length(), 10, '.'); //str = "forgot something in this string.........."
|
erase(index, count) replace(index, count) substr(index, count) | Removes or replaces characters in the string.
| string str = "this is a string";
str.erase(5, str.length()); //str = "is a string"
|