A standardized string object that can be used in place of character arrays
string str;
- This invokes the default constructor and makes str an empty string
string str (s);
- This creates and initializes str to contain a copy of s
- s may be a string or char array
string str (charAr, n);
- This creates and initializes str to contain a copy of the 1st
n characters of charAr
getline(cin, str)
- This extracts characters from cin and stores them in str until:
-- Newline character is found
OR
-- End of file is reached
getline(cin, str, delimiter)
- This extracts characters from cin and stores them in str until:
-- The delimiter is found (not stored in str)
OR
-- End of file is reached
cin >> str
- This extracts characters from cin and stores them in str until:
-- Whitespace is found
OR
-- End of file is reached
cout << str - This inserts characters from str into cout
Assignment Operator Format: str = value;
- Assigns a copy of value to str
-- value may be a string, char array, char
+ Operator Format: str + value or value + str
- returns the result of concatenating str and value
-- value may be a string, char array, char
+= Operator Format: str += value;
- Appends a copy of value to str
-- value may be a string, char array, char
Subscript Notation Format: str[p]
- returns a reference to the character stored in str at position p
str < s - returns true if str is alphabetically less than s - returns false if str is alphabetically greater than or equal to s - s may be a string or char array str <= s - returns true if str is alphabetically less than or equal to s - returns false if str is alphabetically greater than s - s may be a string or char array str > s - returns true if str is alphabetically greater than s - returns false if str is alphabetically less than or equal to s - s may be a string or char array str >= s - returns true if str is alphabetically greater than or equal to s - returns false if str is alphabetically less than s - s may be a string or char array str == s - returns true if str is equal to s - returns false if str is not equal to s - s may be a string or char array str != s - returns true if str is not equal to s - returns false if str is equal to s - s may be a string or char array
string class counterpart to strcpy:
str.assign(s)
- copies s into str
- s may be a string or char array
string class counterparts to strncpy:
str.assign(s, p, n)
- copies n characters from s into str, starting at position p
- s is a string
str.assign(charAr, n)
- copies n characters from charAr into str
- charAr is a char array
string class counterpart to strcat:
str.append(s)
- Appends a copy of s onto str
- s may be a string or char array
string class counterpart to strncat:
str.append(s, p, n)
- appends n characters from s onto str, starting at position p
- s is a string
str.append(charAr, n)
- appends n characters from charAr onto str
- charAr is a char array
string class counterpart to strcmp:
str.compare(s)
- compares str to s
- returns: 0 if str is equal to s
negative value if str < s
positive value if str > s
- s is a string or char array
string class counterpart to strchr and strstr:
str.find(s)
- returns position (subscript) of the first occurrence of s in str
- s is a string, char array, or char
string class counterpart to strlen:
str.length() or str.size()
- returns the length of str
str.c_str()
- converts the string object str to a null terminated char array
- returns: const char *
str.substr(p, n)
- returns a copy of a sub-string n characters long, starting at
position p