☕ Java

String-Methoden – vollständiges Handbuch in Java: Ausführliche Anleitung

What is it and why? (real-life analogy)

Imagine you work in a library. You have a book — it's a string of text. But the book can be in different states: hardcover, with bookmarks, with margin notes. You can do different things with the book: flip pages, search for a specific word, replace old pages with new ones, cut out a piece of text, or glue two books together.

In programming, a String is the same book. It's a sequence of characters: letters, numbers, spaces, punctuation marks. And string methods are those very actions you can perform with this "book." Methods are built-in functions that are already written for you. You don't need to figure out how to find a word in text or how to make all letters uppercase — Java already knows how to do everything.

💡 Important to understand: strings in Java are objects. Every object has methods. When you write a dot after a string name, you're saying: "Hey string, do something with your content!"

Why is this needed? Imagine you're writing a password checking program. You need to find out if it's longer than 8 characters, if it contains digits, if it matches the username. Without string methods, you'd have to write dozens of lines of code, iterating through each character manually. With methods — it's one or two commands.

Theory: how it works internally

Let's look under the hood. When you write String text = "Hello";, Java does several important things:

  1. Creates an object of the String class in a special memory area — the heap.
  2. Stores a character array inside this object. A string is essentially an array of type char[]. Each letter, space, or sign is a separate element of this array.
  3. Fixes the length — the number of characters in the array.

The most important feature of strings in Java is that they are immutable. This means that when you call a method that "changes" the string, a new string is actually created. The old one remains untouched.

⚠️ Remember once and for all: String is an immutable type. Any method that looks like it "modifies" the string (replacement, deletion, conversion to uppercase) returns a new string. The original never changes.

Why is it done this way? Firstly, it's safe. If you passed a string to a method, you're sure the method won't spoil it. Secondly, it allows Java to optimize memory: identical strings can be stored in one place (this is called the string pool).

Inside the String class, there are about 70 methods. We'll cover the most important ones that you'll use in 99% of cases.

Syntax in Java: all forms and variants

String methods are called via a dot. General syntax:

result = string.methodName(arguments);

Where result is what the method returns (a number, a new string, a boolean value), and arguments are the data you pass to the method (for example, a character to search for or a new substring).

Here is the complete table of the most popular methods. Keep it handy — it will come in useful:

Method What it does What it returns Example
length() Counts the number of characters int (integer) "Cat".length() → 3
charAt(int index) Returns the character at the specified position char (single character) "Cat".charAt(1) → 'a'
toUpperCase() Converts all letters to uppercase String (new string) "cat".toUpperCase() → "CAT"
toLowerCase() Converts all letters to lowercase String (new string) "Cat".toLowerCase() → "cat"
equals(Object obj) Compares strings by content boolean (true/false) "cat".equals("cat") → true
equalsIgnoreCase(String str) Compares strings ignoring case boolean "Cat".equalsIgnoreCase("cat") → true
contains(CharSequence s) Checks if the string contains a substring boolean "Hello".contains("ell") → true
startsWith(String prefix) Checks if the string starts with a substring boolean "Java".startsWith("Ja") → true
endsWith(String suffix) Checks if the string ends with a substring boolean "Java".endsWith("va") → true
indexOf(String str)