Arrays & Strings · Lesson 41 of 150
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.
01What 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.
02Why 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.
03Real-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.
04Syntax
javastr.length(); str.toUpperCase(); str.toLowerCase(); str.substring(start, end); str.charAt(index); str.contains("text");
05Example Program
javapublic 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: Hello06Key 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.