Skip to content
C

String Methods

String methods are built-in functions that let you work with String values — like finding their length, changing their case, extracting a portion, or checking if they contain certain text.


1. What are String Methods?

String methods are built-in functions that let you work with String values — like finding their length, changing their case, extracting a portion, or checking if they contain certain text.

2. Why is it used?

Text processing is extremely common — validating a username's length, converting text to uppercase for display, or checking if a search term appears in a sentence. String methods make all of this possible without writing that logic yourself.

3. Real-Life Example

Think of a word processor's "Find" and "Replace" tools, or the button that converts selected text to all capitals. String methods give a Java program that same kind of built-in text-handling power.

4. Syntax

java
str.length(); str.toUpperCase(); str.toLowerCase(); str.substring(start, end); str.charAt(index); str.contains("text");

5. Example Program

java
public class StringMethodDemo { public static void main(String[] args) { String text = "Hello Java"; System.out.println("Length: " + text.length()); System.out.println("Uppercase: " + text.toUpperCase()); System.out.println("Substring: " + text.substring(0, 5)); } }

Output:

Length: 10
Uppercase: HELLO JAVA
Substring: Hello

6. Key Points to Remember

  • None of these methods change the original String — they always return a new one, since strings are immutable.
  • substring(start, end) includes the start index but excludes the end index.
  • These methods are used constantly in real applications, so getting comfortable with them early is worthwhile.