Kodekraftt
/Blog
/String Library Functions In Java Methods Examples And Usage
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

String Library Functions in Java — Methods, Examples and Usage

Learn the most commonly used String library functions in Java with clear explanations, examples, and use cases to write efficient and readable programs.

String Library Functions in Java

In Java, String is one of the most widely used classes. Java provides a rich set of built-in string library functions that make string manipulation simple, readable, and efficient.

Understanding these methods is essential for beginners, interview preparation, and real-world application development.


1. length()

Returns the number of characters in a string.


String s = "Java";
System.out.println(s.length()); // 4

2. charAt(int index)

Returns the character at a specific index.


String s = "Hello";
System.out.println(s.charAt(1)); // e

3. toUpperCase()

Converts all characters to uppercase.


String s = "java";
System.out.println(s.toUpperCase()); // JAVA

4. toLowerCase()

Converts all characters to lowercase.


String s = "JAVA";
System.out.println(s.toLowerCase()); // java

5. equals()

Compares the content of two strings.


String a = "Java";
String b = "Java";
System.out.println(a.equals(b)); // true

6. equalsIgnoreCase()

Compares two strings ignoring case.


System.out.println("Java".equalsIgnoreCase("java")); // true

7. compareTo()

Compares strings lexicographically.


System.out.println("apple".compareTo("banana")); // negative value

8. contains()

Checks if a string contains another sequence.


String s = "programming";
System.out.println(s.contains("gram")); // true

9. startsWith()

Checks if a string starts with a given prefix.


System.out.println("JavaScript".startsWith("Java")); // true

10. endsWith()

Checks if a string ends with a given suffix.


System.out.println("file.txt".endsWith(".txt")); // true

11. substring()

Extracts part of a string.


String s = "Programming";
System.out.println(s.substring(0, 7)); // Program

12. indexOf()

Returns the first occurrence index of a character or substring.


String s = "banana";
System.out.println(s.indexOf('a')); // 1

13. lastIndexOf()

Returns the last occurrence index.


System.out.println("banana".lastIndexOf('a')); // 5

14. replace()

Replaces characters or substrings.


String s = "Java is fun";
System.out.println(s.replace("fun", "powerful"));

15. trim()

Removes leading and trailing spaces.


String s = "  hello  ";
System.out.println(s.trim());

16. split()

Splits a string into an array.


String s = "Java is awesome";
String[] words = s.split(" ");

Important Notes

  • Strings in Java are immutable.
  • Most methods return a new string.
  • Understanding these functions reduces manual logic.

Practice Challenges

  1. Reverse a string using library methods only.
  2. Check palindrome using built-in functions.
  3. Count words using split().

Mastering string library functions is a must for writing clean, efficient, and professional Java code.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.