Assuming declaration: string Var;
-
Function/Operation Description Var = string2
Var.assign("string-to-assign")Assignment of value to string. When assigning a C "char" data type, first check if NULL to avoid failure/crash.
i.e.: if( szVar ) sVar.assign( szVar );
where szVar is a C "char *" data type and sVar is of type "string".Var.swap(string2)
swap(string1,string2)Swap with value held in string2.
Function swap will exchange contents of two string class variables.Var += string2
Var.append()
Var.push_back()Append string/characters. Var.insert() Insert characters Var.erase()
Var = ""Clear string variable. No arguments necessary. + Concatenate ==, !=, <, <=, >, >= Compare strings. Var.compare(string)
Var.compare( size_t pos1, size_t len, string ) const;
Var.compare( size_t pos1, size_t len1, const string, size_t pos2, size_t len2 ) const;Compare strings. Returns int: - 0: if equal.
- -1: Not equal. 1st non matching character in Var is less in value based on ASCII table than in compare string.
- +1: Not equal. 1st non matching character is greater in value based on ASCII table.
Var.length() Return length of string. No arguments necessary. The methods length(), size() and capacity() all return the same value. Var.size() Return length of string. No arguments necessary. Var.capacity() Return length of string + 1. Red Hat 7.x. Red Hat 8.0+ returns the number of characters without the "+1". Number of characters that can be held without re-allocation.
No arguments necessary.Var.max_size() Returns a very large number. No arguments necessary. Var.empty() Returns 1 if an empty string.
Returns 0 if not empty.<< Output stream >>
getline()Input stream Var.c_str() Returns C string pointer. C char string is null terminated. Do not free memory using this pointer! Var.data() Returns C string pointer. C char string is NOT null terminated. Do not free memory using this pointer! Var[]
Var.at(integer)Access individual characters. Return single character at specified position (integer). Var.find(string)
Var.find(string, positionFirstChar)
Var.find(string, positionFirstChar, len)Find first occurance of string or substring. Returns int position of first occurance in string. Where len is the length of the sequence to search for.
Returns string::npos if not found.
i.e. if(Var.find("abc") == string::npos) cout << "Not found" << endl;Var.rfind() Find last occurance of string or substring. Var.find_first_of(string, position)
Var.find_first_of( string, size_t position, size_t len )Find strings and substrings.
Where string is another STL string or null terminated C string.
If position = 0, than start at beginning of string.Var.find_last_of() Find strings and substrings. Var.find_first_not_of()
Var.find_last_not_of()Find strings and substrings. Var.replace(pos1, len1, string)
Var.replace(itterator1, itterator2, const string)
Var.replace(pos1, len1, string, pos2, len2)Replace section of string with new characters.
pos2 and len2 are given when using only a substring of string. Where string is another STL string or null terminated C string.Var.substr(pos, len) Return substring of text given a start position in string object and length. Var.begin()
Var.end()Iterators Var.rbegin()
Var.rend()Reverse iterators
Tidak ada komentar:
Posting Komentar