Operations on Strings in Java

Understanding Operations on Strings

String operations involve various manipulations such as concatenation, reversal, and comparison.

We will explore three different methods to perform operations on strings in Java.

Method 1: Using Standard Library Functions

This method uses built-in string functions for operations.

public class StringOperations {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = " World";
        str1 += str2;
        System.out.println("Concatenated String: " + str1);
        System.out.println("Length of String: " + str1.length());
    }
}
            
Output: Concatenated String: Hello World
Length of String: 11

Method 2: Using Character Array Manipulation

This method manually processes each character for operations.

public class ReverseString {
    public static void main(String[] args) {
        String str = "Hello";
        char[] charArray = str.toCharArray();
        int left = 0, right = charArray.length - 1;
        while (left < right) {
            char temp = charArray[left];
            charArray[left] = charArray[right];
            charArray[right] = temp;
            left++;
            right--;
        }
        System.out.println("Reversed String: " + new String(charArray));
    }
}
            
Output: Reversed String: olleH

Method 3: Using Pointer Manipulation (Character Comparison)

This method performs operations using character comparison.

public class CompareStrings {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        if (str1.equals(str2)) {
            System.out.println("Strings are equal");
        } else {
            System.out.println("Strings are not equal");
        }
    }
}
            
Output: Strings are equal
Strings

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Check whether a character is a vowel or consonant: C C++ Java Python

Check whether a character is an alphabet or not: C C++ Java Python

Find the ASCII value of a character: C C++ Java Python

Length of the string without using strlen() function: C C++ Java Python

Toggle each character in a string: C C++ Java Python

Count the number of vowels: C C++ Java Python

Remove the vowels from a string: C C++ Java Python

Check if the given string is Palindrome or not: C C++ Java Python

Print the given string in reverse order: C C++ Java Python

Remove all characters from string except alphabets: C C++ Java Python

Remove spaces from a string: C C++ Java Python

Replace a sub-string in a string: C C++ Java Python

Count common sub-sequences in two strings: C C++ Java Python

Compare two strings with wildcard support in one of them: C C++ Java Python

List all permutations of a given string in dictionary order: C C++ Java Python

Operations on Strings: C C++ Java Python