VOOZH about

URL: https://www.geeksforgeeks.org/dsa/basic-string-operations-with-implementation/

⇱ Basic String Operations with Implementation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Basic String Operations with Implementation

Last Updated : 13 Mar, 2026

Strings are one of the most commonly used data types in programming. In Java, a String represents a sequence of characters and provides many built-in methods to manipulate and process text data efficiently.

  • Strings in Java are immutable, meaning their values cannot be changed after creation.
  • The String class provides many built-in methods to perform common operations on text data.

Some Common String Operation

Let’s discuss some common String operations in Java along with their implementation.

Accessing characters by index in a string.

To access any character in a String, we need:

  1. A non-empty string (say "s")
  2. A position/index of the character from where it is to be accessed. (say "k")

Using this Approch the character can be easily accessed using the below syntax:

char ch = s.charAt(k);

Below is the implementation of the above approach:

Inserting Character/String into an String.

To insert any Character/String in a String, we need:

  1. A character/string that is to be inserted in the string (say "ch")
  2. A position/index of the Character/String where it is to be inserted. (say "k")

Below is the implementation of the above approach:


Output
Original String : GeeksGeeks 
Modified String : GeeksforGeeks 

Modifying character in String

To modify any Character in a String, we need:

  1. A character that is to replaced in the string (say "ch")
  2. A position/index of the Character where it is to be replaced at. (say "k")

Below is the implementation of the above approach:


Output
Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks

Deletion of character in String

To delete any Character in a String, we need:

  • A character that is to deleted in the string (say "ch")

Below is the implementation of the above approach:


Output
eeksforeeks

Concatenating strings (combining multiple strings into one).

To concatenate any String to a String, we need:

  • A string that is to appended with the string (say "ch")

Below is the implementation of the above approach:


Output
this is init added now

Finding the length/size of a string

To find the length of the String, we need:

  • A string for which the length/size is to be determined (say "str")

Below is the implementation of the above approach:


Output
13

Comparing Strings for Equality

String comparison is used to check if two strings are equal or to determine their lexicographic order.

  • Equal: Both strings have exactly the same characters.
  • Not Equal: Strings differ in one or more characters.
  • Lexicographic Order: Determines which string comes first in dictionary order.
Comment
Article Tags:
Article Tags: