String

Estimated reading: 3 minutes 206 Views

In C++, a string is a sequence of characters represented using the string class. Unlike C-style strings, which are arrays of characters, C++ strings are dynamic and provide a higher level of abstraction. The string class is part of the C++ Standard Template Library (STL), making it convenient and powerful for handling strings.

Declaring and Initializing Strings:

You can declare a string using the string keyword and initialize it in various ways:

// Declare and initialize strings
string str1 = "Hello, ";  // Initialization using a string literal
string str2("world!");    // Initialization using a constructor

// Concatenate strings
string result = str1 + str2;

When you use a string in C++, you must include its library this way:

#include <string>

Example:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string x = "Ahmad", y;
    y.assign(x);
    cout << "y = " << y << endl;

    return 0;
}

String Assignment:

  • string y;: Declares another string variable y.
  • y.assign(x);: Uses the assign method to copy the contents of string x to string y.

Output:

y = Ahmad

String Methods in C++:

Below is an explanation of various string methods in C++, along with their parameters and examples:

  1. at Method:
    Description: Returns the character at a specified position in the string.
    Parameters: Takes an index as a parameter.
    Example:

    string x = "Ahmad";
    cout << x.at(0) << endl;
    
    // A
  2. length Method:
    Description: Returns the number of characters in the string.
    Parameters: No parameters.
    Example:

     string x = "Ahmad";
     cout << x.length() << endl;
    
     // 5
  3. size Method:
    Description: Same as length, returns the number of characters in the string.
    Parameters: No parameters.
    Example:

    string x = "Ahmad";
    cout << x.size() << endl;
    
    // 5
  4. substr Method:
    Description: Returns a substring of the original string.
    Parameters: Takes starting index and length as parameters.
    Example:

    string x = "Ahmad";
    cout << x.substr(1,3) << endl;
    
    // hma
  5. swap Method:
    Description: Swaps the content of two strings.
    Parameters: Takes another string as a parameter.
    Example:

    string x = "Ahmad", y = "Ali";
    cout << x.swap(y) << endl;
    
    // Swaps the content of x and y
  6. find Method:
    Description: Finds the first occurrence of a substring in the string.
    Parameters: Takes a substring as a parameter.
    Example:

    string x = "Ahmad";
    cout << x.find('a') << endl;
    
    // 3
  7. rfind Method:
    Description: Finds the last occurrence of a substring in the string.
    Parameters: Takes a substring as a parameter.
    Example:

    string x = "Ahmad";
    cout << x.rfind('a') << endl;
    
    // 3
  8. erase Method:
    Description: Erases characters from the string.
    Parameters: Takes starting index and count as parameters.
    Example:

    string x = "Ahmad";
    cout << x.erase(0,3) << endl;
    
    // ad
  9. replace Method:
    Description: Replaces a portion of the string with another string.
    Parameters: Takes starting index, length, and replacement string as parameters.
    Example:

    string x = "Ahmad";
    cout << x.replace(1,4,"li") << endl;
    
    // Ali
  10. insert Method:
    Description: Inserts characters into the string.
    Parameters: Takes starting index and string to insert as parameters.
    Example:

    string x = "Ahmad";
    cout << x.insert(5," Ali") << endl;
    
    // Ahmad Ali

These are some commonly used string methods in C++. Remember to include the <string> header for using these methods.

Share this

String

Or copy link

CONTENTS
English