Basic String Operations

Cards (17)

  • Assignment Operation (=)
    string str1 = "Welcome";
    string str2;
    str2 = str1; // assign the string literal of str1 to str2
    str1 = string("Jones"); //or str1 = "Jones“;
  • Assignment Operation (=)
    string str1 = "Welcome";
    string str2;
    str2 = str1; // assign the string literal of str1 to str2
    str1 = string("Jones"); //or str1 = "Jones“;
  • Assignment Operation using assign() method
    string str1 = "Welcome";
    string str2 = “Hello";
    str1.assign(str2); // assign the string literal of str2 to str1
    cout << str1; //display Hello
  • Assignment Operation using assign() method
    string str1 = "Welcome";
    string str2 = “Hello";
    str1.assign(str2); // assign the string literal of str2 to str1
    cout << str1; //display Hello
  • Concatenation Operation (+)
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    string str3 = str1 + " " + str2; //the value of str3 is Welcome to C++ Programming!
    str1 += str2; //str1 is WelcometoC++Programming!
    str3 = "Welcome" + "to C++ Programming!"; //illegal statement concatenation works if there is at least 1 string data type like str3 = str1 + "Mickey";
  • Concatenation Operation (+)
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    string str3 = str1 + " " + str2; //the value of str3 is Welcome to C++ Programming!
    str1 += str2; //str1 is WelcometoC++Programming!
    str3 = "Welcome" + "to C++ Programming!"; //illegal statement concatenation works if there is at least 1 string data type like str3 = str1 + "Mickey";
  • Concatenation Operation (+)
    string x = "10";
    string y = "20";
    string z = x + y;
    cout << z << endl; //the output is 1020
  • Concatenation Operation (+)
    string x = "10";
    string y = "20";
    string z = x + y;
    cout << z << endl; //the output is 1020
  • Convert string to integer (stoi)
    string x = "10";
    string y = "20";
    int z = stoi(x) + stoi(y);
    cout << z << endl; //the output is 30
  • Convert string to integer (stoi)
    string x = "10";
    string y = "20";
    int z = stoi(x) + stoi(y);
    cout << z << endl; //the output is 30
  • Other conversion for numeric values:
    stol() string to long
    stoll() string to long long
    stof() string to float
    stod() string to double
    stold() string to long double
  • String Length/Size Method/Function
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    int len = str1.length(); //or str1.size(); // len is 7
    int stringLen = str1.size() + str2.length(); //stringLen is 26
  • String Length/Size Method/Function
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    int len = str1.length(); //or str1.size(); // len is 7
    int stringLen = str1.size() + str2.length(); //stringLen is 26
  • Access String Characters
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    cout << str1[0]; //display W
    cout << str1[3]; //display c
    cout << str1[str1.length() - 1]; //display e or last character of the string literal
    str2[0] = ‘J’; //str2 is Jo C++ Programming!
  • Access String Characters
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    cout << str1[0]; //display W
    cout << str1[3]; //display c
    cout << str1[str1.length() - 1]; //display e or last character of the string literal
    str2[0] = ‘J’; //str2 is Jo C++ Programming!
  • Access String Characters using at() method
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    cout << str1.at(0); //display W
    cout << str1.at(3); //display c
    cout << str1.at(str1.size() – 1); //display e or the last character of the string literal
    str2.at(0) = ‘J’; //str2 is Jo C++ Programming!
  • Access String Characters using at() method
    string str1 = "Welcome";
    string str2 = "to C++ Programming!";
    cout << str1.at(0); //display W
    cout << str1.at(3); //display c
    cout << str1.at(str1.size() – 1); //display e or the last character of the string literal
    str2.at(0) = ‘J’; //str2 is Jo C++ Programming!