C++ String 1. C-Style String A C-Style string is any null-terminated byte string (NTBS), where this is a sequence of nonzero bytes followed by a byte with zero (0) value (the terminating null character).
terminating null character: '\0' length of an NTBS is the number of elements that precede the terminating null character. An empty NTBS has a length of zero. size of an NTBS is the size of the entire array, including the terminating null character. A single quotes (') are used to identify character literals. A double quotes ('') are used to identify string literals. String literals are stored in your program image, usually in a read-only section (.data), 1.1. Create a String /// @brief 1.Using pointer char* str1= "abc"; // sizeof(str1) = 32 or 64 (ptr) str1[0] = 1; // error, ptr to const /// @brief 1.Using array char str2[] = "abc"; // sizeof(str2) = 4 str2[1] = 'a'; // OK For str_1, the memory for the array is allocated on the stack at runtime. The compiler initializes it from the string literal. At runtime, the program memory copies the string literal into the array For str_2, only the address of the string literal is held on the stack, and there is no copying of string literal. 1.2. Character Null: \0, 0x00, NULL Carriage Return And New Line: \r\n Case switching: 'A' ^ ' ' & 'a' ^ ' ' Special: \\, Escape sequences: Name Symbol Meaning Alert \a Makes an alert, such as a beep Backspace \b Moves the cursor back one space Formfeed \f Moves the cursor to next logical page Newline \n Moves cursor to next line Carriage return \r Moves cursor to beginning of line Horizontal tab \t Prints a horizontal tab Vertical tab \v Prints a vertical tab Single quote \' Prints a single quote Double quote \" Prints a double quote Backslash \\ Prints a backslash Question mark \? Prints a question mark (no longer relevant) Octal number \{number} Translates into char represented by octal Hex number \x{number} Translates into char represented by hex number 1.3. C String Libraries Copying strings : strcpy, strncpy Concatenating strings: strcat, strncat Comparing strings: strcmp, strncmp Parsing strings: strtok, strcspn Length: strlen #include <stdio.h> #include <string.h> void main() { /// @brief Copying char src[] = "Hello"; char dst[20]; strcpy(dst, src); // copy full string // dst = "Hello" strncpy(dst, "World", 3); // copy only 3 chars dst[3] = '\0'; // ensure null-termination // dst = "Wor" /// @brief Concatenating strings char text[20] = "Hi"; strcat(text, " there"); // append full string // text = "Hi there" strncat(text, "!!!", 2); // append only 2 chars // text = "Hi there!!" /// @brief Comparing strings int r1 = strcmp("abc", "abc"); // r1 = 0 (equal) int r2 = strcmp("abc", "abd"); // r2 < 0 (abc < abd) int r3 = strncmp("abcdef", "abcxyz", 3); // r3 = 0 (first 3 chars equal) /// @brief Parsing strings char line[] = "A,B,C"; char* token = strtok(line, ","); // first token: "A" while (token != NULL) { printf("token: %s\n", token); token = strtok(NULL, ","); } // strcspn: find first occurrence of any chars in reject set char sample[] = "hello123world"; size_t pos = strcspn(sample, "0123456789"); // pos = 5 (first digit is at index 5) /// @brief Length size_t len = strlen("abc"); // len = 3 } 1.4. String/Numbers Conversion Integer to String: itoa() (non-standard) String to Double: atof() String to Double (with error checking): strtod() String to Long (with base + error checking): strtol() #include <stdio.h> #include <stdlib.h> // atof, strtod, strtol #include <string.h> // itoa (non-standard on some compilers) void main() { /// @brief Integer to String (itoa) char buf[32]; itoa(1234, buf, 10); // convert integer to string in base 10 // buf = "1234" itoa(255, buf, 16); // convert to hex // buf = "ff" /// @brief String to Double (atof) double d1 = atof("3.14159"); // d1 = 3.14159 double d2 = atof("12.5xyz"); // d2 = 12.5 (atof stops at non-numeric chars) /// @brief String to Double (strtod) char* end; double d3 = strtod("45.67abc", &end); // d3 = 45.67 // end -> "abc" /// @brief String to Long (strtol) long v1 = strtol("1234", NULL, 10); // v1 = 1234 (decimal) long v2 = strtol("FF", NULL, 16); // v2 = 255 (hex to decimal) char* end2; long v3 = strtol("100xyz", &end2, 10); // v3 = 100 // end2 -> "xyz" return 0; } 2. C++ String Strings are objects that represent sequences of characters. <string>
...