Launch your tech mastery with us—your coding journey starts now!
Course Content
Data Structure

String Operations

Because Strings are immutable, simple operations can be costly if done incorrectly. Here is how to handle them. String operations infographic showing concatenation, substring extraction, length calculation, string reversal, character replacement, and string splitting with visual examples.

Common Operations:

  1. Concatenation: Joining two strings together.
  2. Substring: Extracting a portion of a string.
  3. Length: Counting the number of characters.
  4. Searching: Finding the index of a specific character or pattern.

Code Implementation: String Methods

JavaPythonCC++

public class StringBasics {
    public static void main(String[] args) {
        // 1. Creation
        String str1 = "Hello";              // String Literal (Stored in Pool)
        String str2 = new String("World");  // String Object (Stored in Heap)

        // 2. Concatenation
        String result = str1 + " " + str2; 
        System.out.println(result); // Output: Hello World

        // 3. Length (O(1))
        System.out.println("Length: " + result.length()); // Output: 11

        // 4. CharAt (Accessing character at index - O(1))
        System.out.println("Char at index 1: " + str1.charAt(1)); // Output: e

        // 5. Substring (Extracting part - O(n))
        // substring(start, end) -> includes start, excludes end
        System.out.println("Substring: " + result.substring(0, 5)); // Output: Hello

        // 6. Equality Check (Critical in Java!)
        // ALWAYS use .equals(), NEVER use == for strings
        System.out.println(str1.equals("Hello")); // true
    }
}

# String Basics

# 1. Creation
str1 = "Hello"
str2 = "World"

# 2. Concatenation
result = str1 + " " + str2
print(result)  # Output: Hello World

# 3. Length
print("Length:", len(result))  # Output: 11

# 4. Access character at index
print("Char at index 1:", str1[1])  # Output: e

# 5. Substring (Slicing)
# includes start, excludes end
print("Substring:", result[0:5])  # Output: Hello

# 6. Equality Check
print(str1 == "Hello")  # True

#include <stdio.h>
#include <string.h>

int main() {

    // 1. Creation
    char str1[] = "Hello";
    char str2[] = "World";

    // 2. Concatenation
    char result[20];
    strcpy(result, str1);
    strcat(result, " ");
    strcat(result, str2);

    printf("%s\n", result); // Output: Hello World

    // 3. Length
    printf("Length: %lu\n", strlen(result));

    // 4. Access character at index
    printf("Char at index 1: %c\n", str1[1]);

    // 5. Substring
    char substring[6];
    strncpy(substring, result, 5);
    substring[5] = '\0';
    printf("Substring: %s\n", substring);

    // 6. Equality Check
    printf("%s\n", strcmp(str1, "Hello") == 0 ? "true" : "false");

    return 0;
}

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

int main() {

    // 1. Creation
    string str1 = "Hello";
    string str2 = "World";

    // 2. Concatenation
    string result = str1 + " " + str2;
    cout << result << endl; // Output: Hello World

    // 3. Length
    cout << "Length: " << result.length() << endl;

    // 4. Access character at index
    cout << "Char at index 1: " << str1[1] << endl;

    // 5. Substring
    cout << "Substring: " << result.substr(0, 5) << endl;

    // 6. Equality Check
    cout << (str1 == "Hello") << endl;

    return 0;
}