String Operations (Concatenation, Comparison, Substring, Length) in C++


Strings are a crucial part of programming in C++, and the C++ string class provides several built-in operations for manipulating and working with strings. In this article, we will cover basic string operations such as concatenation, comparison, extracting substrings, and finding the length of a string, with examples in C++.

1. String Concatenation

Concatenation is the operation of combining two or more strings into one string. In C++, you can concatenate strings using the + operator or the append() method of the string class.

Example of String Concatenation Using + Operator

    #include <iostream>
    #include <string>

    int main() {
        std::string str1 = "Hello";
        std::string str2 = " World";
        
        // Concatenation using the + operator
        std::string result = str1 + str2;
        
        std::cout << "Concatenated string: " << result << std::endl;

        return 0;
    }
        

In this example:

  • str1 + str2 concatenates the strings "Hello" and " World", resulting in "Hello World".

Example of String Concatenation Using append() Method

    #include <iostream>
    #include <string>

    int main() {
        std::string str1 = "Hello";
        std::string str2 = " World";
        
        // Concatenation using append() method
        str1.append(str2);
        
        std::cout << "Concatenated string: " << str1 << std::endl;

        return 0;
    }
        

In this example:

  • The append() method is used to append str2 to str1.

2. String Comparison

String comparison in C++ can be done using the == operator or the compare() method. The == operator compares two strings for equality, while the compare() method allows more detailed comparison.

Example of String Comparison Using == Operator

    #include <iostream>
    #include <string>

    int main() {
        std::string str1 = "Hello";
        std::string str2 = "Hello";
        std::string str3 = "World";

        if (str1 == str2) {
            std::cout << "str1 is equal to str2" << std::endl;
        } else {
            std::cout << "str1 is not equal to str2" << std::endl;
        }

        if (str1 == str3) {
            std::cout << "str1 is equal to str3" << std::endl;
        } else {
            std::cout << "str1 is not equal to str3" << std::endl;
        }

        return 0;
    }
        

In this example:

  • The == operator compares str1 with str2 and str1 with str3.
  • It prints whether the strings are equal or not.

Example of String Comparison Using compare() Method

    #include <iostream>
    #include <string>

    int main() {
        std::string str1 = "Hello";
        std::string str2 = "Hello";
        std::string str3 = "World";

        // Using compare() method
        if (str1.compare(str2) == 0) {
            std::cout << "str1 is equal to str2" << std::endl;
        }

        if (str1.compare(str3) != 0) {
            std::cout << "str1 is not equal to str3" << std::endl;
        }

        return 0;
    }
        

In this example:

  • The compare() method returns 0 if the strings are equal, a negative value if the first string is lexicographically smaller, and a positive value if the first string is lexicographically greater.

3. Extracting Substrings

In C++, substrings can be extracted from a string using the substr() method. The substr() method allows you to specify the starting index and the length of the substring.

Syntax for substr() Method

    string substr (size_t pos = 0, size_t len = npos);
        

Example of Extracting a Substring

    #include <iostream>
    #include <string>

    int main() {
        std::string str = "Hello, World!";
        
        // Extracting a substring starting from index 7 with length 5
        std::string subStr = str.substr(7, 5);
        
        std::cout << "Extracted substring: " << subStr << std::endl;

        return 0;
    }
        

In this example:

  • str.substr(7, 5) extracts a substring starting from index 7 and takes the next 5 characters, resulting in the substring "World".

4. Finding the Length of a String

The length of a string can be found using the length() or size() method. Both methods return the number of characters in the string, excluding the null terminator.

Example of Finding String Length

    #include <iostream>
    #include <string>

    int main() {
        std::string str = "Hello, World!";
        
        // Finding the length of the string
        std::cout << "Length of the string: " << str.length() << std::endl;

        return 0;
    }
        

In this example:

  • str.length() returns the length of the string "Hello, World!", which is 13.

5. Conclusion

In C++, the string class provides powerful and easy-to-use methods for performing common string operations such as concatenation, comparison, extracting substrings, and finding the length. These operations make it easier to manipulate and work with strings in C++ programs. The string class abstracts away the complexities of working with character arrays, making string manipulation more straightforward and efficient.





Advertisement