C++ String Methods/Functions

Cards (23)

  • empty() method check if a string is empty or not. It will return 1 if the string is empty, otherwise 0.
  • empty() Syntax:
    stringVar.empty();
    string str1 = "Welcome";
    string str2;
    bool x = str2.empty();
    cout << x; // display 1
  • empty() Syntax:
    stringVar.empty();
    string str1 = "Welcome";
    string str2;
    bool x = str2.empty();
    cout << x; // display 1
  • append() method is used to add a string at the end of another string.
  • append() Syntax:
    stringVar.append(stringVar);
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    str1.append(str2);
    cout << str1; // display Welcome to C++ Programming!
  • append() Syntax:
    stringVar.append(stringVar);
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    str1.append(str2);
    cout << str1; // display Welcome to C++ Programming!
  • append() Syntax:
    stringVar.append(stringVar, pos, len);
    where:
    pos is the starting position to append, index base 0
    len is the number of character/s to append, it start with 1
  • append() Syntax:
    stringVar.append(stringVar, pos, len);
    where:
    pos is the starting position to append, index base 0
    len is the number of character/s to append, it start with 1
  • Example for append() method:
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    str1.append(str2, 3, 3); //the position is C, then 3 characters to append which is C++
    cout << str1; // display Welcome C++
  • Example for append() method:
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    str1.append(str2, 3, 3); //the position is C, then 3 characters to append which is C++
    cout << str1; // display Welcome C++
  • Example for append() method:
    string str1 = “Hello ";
    str1.append(“World”); //str1 is Hello World
    cout << str1; // display Hello World
  • Example for append() method:
    string str1 = “Hello ";
    str1.append(“World”); //str1 is Hello World
    cout << str1; // display Hello World
  • Example of append() method:
    string str1 = “Hello ";
    str1.append(“World”, 1, 3); //str1 is Hello orl
    //the position is 1, then 3 characters to append which is orl
    cout << str1; // display Hello orl
  • Example for append() method:
    string str1 = “Hello"; str1.append(3, ‘!’); //str1 value is Hello!!!
    //will append 3 characters of !
    cout << str1; // display Hello!!!
  • insert() method is used to insert characters or substrings into a string at a specific position.
  • insert() method is used to insert characters or substrings into a string at a specific position.
  • insert() Syntax:
    stringVar.insert(pos, string/stringVar);
    where:
    pos is the starting position to insert a string, index is base 0
  • insert() Syntax:
    stringVar.insert(pos, string/stringVar);
    where:
    pos is the starting position to insert a string, index is base 0
  • insert() Syntax:
    stringVar.insert(pos, string/stringVar);
    where:
    pos is the starting position to insert a string, index is base 0
  • Example for insert() method:
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    str1.insert(8, "Mickey");
    cout << str1; // display Welcome Mickey
  • Example for insert() method:
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    str1.insert(8, "Mickey");
    cout << str1; // display Welcome Mickey
  • Example for insert() method:
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    str1.insert(7, "Mickey");
    cout << str1; // display WelcomeMickey
    cout << str1 << “!”; // display WelcomeMickey !
  • Example for insert() method:
    string str1 = "Welcome ";
    string str2 = "to C++ Programming!";
    string str3 = "Jones“;
    str1.insert(7, str3);
    cout << str1; // display WelcomeJones