
String Functions
find() | Description: find(item) returns index of first item occurrence, else returns string::npos (a constant defined in the string library). Item may be char, string variable, string literal (or char array). find(item, indx) starts at index indx. Ex. // userText is "Help me!" userText.find('p') // Returns 3 userText.find('e') // Returns 1 (first occurrence of e only) userText.find('z') // Returns string::npos userText.find("me") // Returns 5 userText.find('e', 2) // Returns 6 (starts at index 2) |
substr() | Description: substr(index, len) returns substring starting at index and having len characters. Ex. // userText is "http://google.com" userText.substr(0, 7) // Returns "http://" userText.substr(13, 4) // Returns ".com" userText.substr(userText.length() - 4, 4) // Last 4: ".com" |
push_back() | Description: push_back(c) appends character c to the end of a string. Ex. // userText is "Hello" userText.push_back('?'); // Now "Hello?" userText.length(); // Returns 6 |
insert() | Description: insert(indx, subStr) Inserts string subStr starting at index indx. Ex. // userText is "Goodbye" userText.insert(0, "Well "); // Now "Well Goodbye" // userText is "Goodbye" userText.insert(4, "---"); // Now "Good---bye" |
replace() | Description: replace(indx, num, subStr) replaces characters at indices indx to indx+num-1 with a copy of subStr. Ex. // userText is "You have many gifts" userText.replace(9, 4, "a plethora of"); // Now "You have a plethora of gifts" |
str1 + str2 | Description: Returns a new string that is a copy of str1 with str2 appended. If one of str1, str2 is a string, the other may be a character (or character array). Ex. // userText is "A B" myString = userText + " C D"; // myString is "A B C D" myString = myString + '!'; // myString now "A B C D!" myString = myString + userText; // myString now "A B C D!A B" |
Some C string modification functions.
Given:
char orgName[100] = “United Nations”;
char userText[20] = “UNICEF”;
char targetText[10];
strcpy() | Description: strcpy(destStr, sourceStr) Copies sourceStr (up to and including null character) to destStr. Ex. strcpy(targetText, userText); // Copies "UNICEF" + null char // to targetText strcpy(targetText, orgName); // Error: "United Nations" // has > 10 chars targetText = orgName; // Error: Strings can't be // copied this way |
strncpy() | Description: strncpy(destStr, sourceStr, numChars) Copies up to numChars characters. Ex. strncpy(orgName, userText, 6); // orgName is "UNICEF Nations" |
strcat() | Description: strcat(destStr, sourceStr) Copies sourceStr (up to and including null character) to end of destStr (starting at destStr's null character). Ex. strcat(orgName, userText); // orgName is "United NationsUNICEF" |
strncat() | Description: strncat(destStr, sourceStr, numChars) Copies up to numChars characters to destStr's end, then appends null character. Ex. strcpy(targetText, "abc"); // targetText is "abc" strncat(targetText, "123456789", 3); // targetText is "abc123" |
Some C string information functions.
Given:
char orgName[100] = “United Nations”;
char userText[20] = “UNICEF”;
char targetText[10];
strchr() | Description: strchr(sourceStr, searchChar) Returns NULL if searchChar does not exist in sourceStr. (Else, returns address of first occurrence, discussed elsewhere). NULL is defined in the cstring library. Ex. if (strchr(orgName, 'U') != NULL) { // 'U' exists in orgName? ... // 'U' exists in "United Nations", branch taken } if (strchr(orgName, 'u') != NULL) { // 'u' exists in orgName? ... // 'u' doesn't exist (case matters), branch not taken } |
strlen() | Description: size_t strlen(sourceStr) Returns number of characters in sourceStr up to, but not including, first null character. size_t is integer type. Ex. x = strlen(orgName); // Assigns 14 to x x = strlen(userText); // Assigns 6 to x x = strlen(targetText); // Error: targetText may lack null char |
strcmp() | Description: int strcmp(str1, str2) Returns 0 if str1 and str2 are equal, non-zero if they differ. Ex. if (strcmp(orgName, "United Nations") == 0) { ... // Equal, branch taken } if (strcmp(orgName, userText) == 0) { ... // Not equal, branch not taken } |