String - C P P

  • Strings are objects that represent sequences of characters
  • Size is not constant unlike character array
  • std::string stores its data internally in the form of a null-terminated C-string, but in normal usage does not allow you to access the null terminator
  • Most string implementations have a built-in array of 16 characters (so short strings don't fragment the heap) and use the heap for longer strings
  • Basic
    • #include <string> => Header file to include
    • string variableName => Declare a string
    • string variableName = "value" => Initialize a string, Giving it value, Spaces are allowed
    • string variableName(N, 'c') => Initialize a string filled with character 'c' n times
    • cin >> variableName => Take input value of string without spaces
    • getline(cin, variableName); => Get whole line of input
      • cin.getline(variableName, N);
  • Element Access
    • variableName[N] => Access the (N-1)th character
    • variableName.at(N) => Get character in string
    • variableName.back() => Access last character
    • variableName.front() => Access first character
  • Capacity
    • variableName.length() => Returns length of string
    • variableName.size() => Returns length of string
    • variableName.empty() => Returns true if string is empty
    • variableName.clear() => Clear string
    • variableName.max_size() => Return maximum size of string
    • variableName.resize(N) => Resize string to "n"
      • variableName.resize(N, 'val') => Resize string to "n" filling all values with character 'val'
    • variableName.capacity() => Return size of allocated storage
  • Modifiers
    • variableName.push_back("value") => Append characters to string
    • variableName.pop_back() => Delete last character
    • variableName.insert(N, "value") => Insert value at the nth index
    • variableName.erase(N, m) => Removes 'm' characters starting from nth index
    • variableName1.append(variableName2) => Append s2 in the end of s1
      • variableName1 + variableName2 => Append s2 in the end of s1
    • variableName.replace() => Replace portion of string
    • variableName1.swap(variableName2) => Exchange the values of these two string objects
  • Operations
    • variableName.compare() => Compare strings
    • variableName.substr(N) => Returns string starting from Nth index
      • variableName.substr(N, M) => Returns 'M' characters starting from Nth index, util end of the string if it comes first
    • variableName.find("value") => Returns the first index of the substring found
    • variableName1.compare(variableName2) => Returns 0 if equal, Positive value if S1 is greater
    • strlen(variableName) => Returns length of string
    • strcmp(variableName1, variableName2) => Returns true if both array are equal
    • strcpy(variableName1, variableName2) => Copies variableName2 in variableName1
    • to_string(variableName) => Converts an integer into a string
    • stoi(variableName) => Converts a string to integer values into an integer
      • stoi(variableName, N, M) => N is starting position, M is base of number system, Default is 10
    • atoi(variableName) => Converts a character array argument to integer value
    • Transform
      • transform(N, m, o, ::tolower) => Transforms the string in lowercase, Parameters are start index, end index, where to start
      • transform(N, m, o, ::toupper) => Transforms the string in uppercase
    • Sort
      • sort(N, m) => Sorts the characters in ascending, Parameter n is starting and m is ending index
      • sort(N, m, greater()) => Sorts the characters in descending, greater() function returns true when condition is valid
  • Iterators
    • variableName.begin() => Returns the first character of thr string
    • variableName.end() => It returns an iterator pointing to the end of the string
    • variableName.rbegin() => Return reverse iterator to reverse beginning
    • variableName.rend => Return reverse iterator to reverse end
  • Loops
    • For loop
          for (int i = 0; i < variableName.length(); ++i) {
              char ch = variableName[i];
          }
      
    • Range based
          for (char ch : variableName) {
              cout << ch;
          }
      
    • Iterator
          for (string::iterator it = variableName.begin(); it != variableName.end(); ++it) {
              char ch = *it;
          }
          for (string::const_iterator it = variableName.begin(); it != variableName.end(); ++it) {
              char ch = *it;
          }
      
  • String Stream
    • Associates a string object with a stream allowing you to read from the string as if it were a stream
    • Commands
      • stringstream variableName;
      • variableName << value => Store string value into variableName
      • variableName >> variableName2 => Store the value obtained from variableName which is now iterating through variableName into variableName2, Data type should be taken care of
      • variableName.str() => Make string out of variableName
      • variableName.eof()
      • variableName.good()
Share: