Java String Methods
Introduction
In Java, a String is an object that represents a sequence of characters.
It is defined in the java.lang package and Strings are immutable, meaning once created,
their values cannot be changed.
You can create strings in two ways:
String s1 = "Hello"; // using string literal
String s2 = new String("World"); // using new keyword
Commonly Used String Methods
1️. toLowerCase()
Description:
Converts all characters in the string to lowercase.
Syntax:
String s2 = s1.toLowerCase();
Example:
String s1 = "JAVA";
System.out.println(s1.toLowerCase()); // Output: java
2️. toUpperCase()
Description:
Converts all characters in the string to uppercase.
Syntax:
String s2 = s1.toUpperCase();
Example:
String s1 = "java";
System.out.println(s1.toUpperCase()); // Output: JAVA
3️. replace(char oldChar, char newChar)
Description:
Replaces all occurrences of one character with another.
Syntax:
String s2 = s1.replace('a', 'o');
Example:+
String s1 = "banana";
System.out.println(s1.replace('a', 'o')); // Output: bonono
4️. trim()
Description:
Removes white spaces from both the beginning and end of a string.
Syntax:
String s2 = s1.trim();
Example:
String s1 = " Hello ";
System.out.println(s1.trim()); // Output: Hello
5️. equals(String anotherString)
Description:
Checks whether two strings are exactly equal (case-sensitive).
Syntax:
boolean result = s1.equals(s2);
Example:
String s1 = "Java";
String s2 = "java";
System.out.println(s1.equals(s2)); // Output: false
6️. equalsIgnoreCase(String anotherString)
Description:
Compares two strings ignoring case differences.
Syntax:
boolean result = s1.equalsIgnoreCase(s2);
Example:
String s1 = "Java";
String s2 = "java";
System.out.println(s1.equalsIgnoreCase(s2)); // Output: true
7️. length()
Description:
Returns the number of characters in a string.
Syntax:
int len = s1.length();
Example:
String s1 = "Hello";
System.out.println(s1.length()); // Output: 5
8️. charAt(int index)
Description:
Returns the character at the specified index (starts from 0).
Syntax:
char c = s1.charAt(2);
Example:
String s1 = "World";
System.out.println(s1.charAt(2)); // Output: r
9️. compareTo(String anotherString)
Description:
Compares two strings lexicographically.
Returns 0 if equal
Returns positive if s1 > s2
Returns negative if s1 < s2
Syntax:
int result = s1.compareTo(s2);
Example:
String s1 = "Apple";
String s2 = "Banana";
System.out.println(s1.compareTo(s2)); // Output: negative value
10. concat(String str)
Description:
Concatenates one string to another.
Syntax:
String s3 = s1.concat(s2);
Example:
String s1 = "Hello ";
String s2 = "World";
System.out.println(s1.concat(s2)); // Output: Hello World
11️. substring(int beginIndex)
Description:
Returns substring from the specified index to the end.
Syntax:
String s2 = s1.substring(3);
Example:
String s1 = "Programming";
System.out.println(s1.substring(3)); // Output: gramming
12️. substring(int beginIndex, int endIndex)
Description:
Returns substring from beginIndex up to endIndex (excluding endIndex).
Syntax:
String s2 = s1.substring(2, 6);
Example:
String s1 = "Computer";
System.out.println(s1.substring(2, 6)); // Output: mput
13️. indexOf(char ch)
Description:
Returns the position of the first occurrence of the character.
Syntax:
int pos = s1.indexOf('a');
Example:
String s1 = "banana";
System.out.println(s1.indexOf('a')); // Output: 1
14️. indexOf(char ch, int fromIndex)
Description:
Returns the position of the character after the given index.
Syntax:
int pos = s1.indexOf('a', 2);
Example:
String s1 = "banana";
System.out.println(s1.indexOf('a', 2)); // Output: 3
15️. String.valueOf(variable)
Description:
Converts any data type (int, float, object) into a string.
Syntax:
String s = String.valueOf(num);
Example:
int num = 100;
String s = String.valueOf(num);
System.out.println(s + 50); // Output: 10050
16️. toString()
Description:
Returns the string representation of an object.
Syntax:
String s = obj.toString();
Example:
Integer n = 123;
System.out.println(n.toString()); // Output: 123
Definition:
In Java, a Vector is a dynamic array that can grow or shrink in size
automatically when elements are added or removed.
It is a class in the java.util package and is used to store a group of objects.
Simple Definition:
A Vector in Java is a resizable array that can hold objects and automatically
increases its size when needed. It is synchronized, meaning it is safe to use in
multithreaded programs.
The Vector class implements the List interface and provides a way to store
elements in an ordered collection, allowing random access using index
numbers (just like arrays).
Example:
import java.util.Vector;
public class Example
{
public static void main(String[] args)
{
Vector<String> names = new Vector<String>();
names.add("Asha");
names.add("Rahul");
names.add("Priya");
System.out.println(names);
}
}
Output:
[Asha, Rahul, Priya]
⚙️Key Points:
Found in package: java.util
Syntax: Vector<Type> obj = new Vector<Type>();
Automatically resizes when full
Stores only objects (not primitive types)
Synchronized (thread-safe)
Maintains insertion order