🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🔧 AI Nachrichten Stealing AI Reasoning Traces(08.09.2026 um 12:20 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🔧 AI Nachrichten Stealing AI Reasoning Traces(08.09.2026 um 12:20 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Java String Manipulation: Common Methods You Should Know

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

String manipulation is one of the most fundamental operations in Java programming. Whether you're processing user inputs, formatting output, or implementing algorithms, working with strings effectively is a critical skill. Java provides a rich set of methods for manipulating strings, making it easier to perform a wide range of tasks. In this blog, we’ll explore some of the most common string manipulation methods every Java developer should know.









1. Creating Strings



Strings in Java can be created in two ways:





  • Using String literals:




CODE
  String str = "Hello, World!";








  • Using the new keyword:




CODE
  String str = new String("Hello, World!");






While both methods achieve the same result, using string literals is generally preferred due to the string pool optimization in Java.









2. Checking String Length



To find the length of a string, use the length() method:




CODE
String str = "Java Programming";
System.out.println("Length: " + str.length());






Output:


Length: 16







3. Concatenation



You can combine strings using:




  • The + operator:



CODE
  String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);






  • The concat() method:



CODE
  String fullName = firstName.concat(" ").concat(lastName);









4. Comparing Strings



Java offers several ways to compare strings:





  • equals() Method: Compares two strings for equality, considering case sensitivity.



CODE
  String str1 = "Java";
String str2 = "Java";
System.out.println(str1.equals(str2)); // true







  • equalsIgnoreCase() Method: Ignores case sensitivity.



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







  • compareTo() Method: Compares strings lexicographically.



CODE
  System.out.println(str1.compareTo("Jawa")); // Negative value









5. Extracting Substrings



The substring() method is used to extract a portion of a string:




CODE
String str = "Welcome to Java";
System.out.println(str.substring(11)); // "Java"
System.out.println(str.substring(0, 7)); // "Welcome"












6. Searching in Strings





  • indexOf() Method: Finds the first occurrence of a character or substring.




CODE
  String str = "Programming in Java";
System.out.println(str.indexOf('a')); // 12
System.out.println(str.indexOf("Java")); // 16








  • lastIndexOf() Method: Finds the last occurrence.




CODE
  System.out.println(str.lastIndexOf('a')); // 21












7. Replacing Characters or Substrings



Use the replace() method to replace characters or substrings:




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






For advanced replacements, replaceAll() supports regular expressions:




CODE
String str = "123-456-7890";
System.out.println(str.replaceAll("\\d", "*")); // "***-***-****"












8. Splitting Strings



The split() method divides a string into an array based on a delimiter:




CODE
String str = "apple,orange,banana";
String[] fruits = str.split(",");
for (String fruit : fruits) {
System.out.println(fruit);
}






Output:




CODE
apple
orange
banana












9. Changing Case





  • toUpperCase() Method: Converts all characters to uppercase.




CODE
  String str = "hello";
System.out.println(str.toUpperCase()); // "HELLO"








  • toLowerCase() Method: Converts all characters to lowercase.




CODE
  System.out.println(str.toLowerCase()); // "hello"












10. Trimming Whitespace



Use trim() to remove leading and trailing spaces:




CODE
String str = "  Java Programming  ";
System.out.println(str.trim()); // "Java Programming"












11. Checking String Emptiness



Java provides two methods to check if a string is empty or blank:





  • isEmpty() Method: Returns true if the string has no characters.




CODE
  String str = "";
System.out.println(str.isEmpty()); // true








  • isBlank() Method (Java 11+): Returns true if the string is empty or contains only whitespace.




CODE
  String str = "   ";
System.out.println(str.isBlank()); // true












12. String Formatting



The String.format() method allows you to create formatted strings:




CODE
String name = "Alice";
int age = 25;
String message = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(message);






Output:


My name is Alice and I am 25 years old.







13. Joining Strings



From Java 8 onward, the String.join() method simplifies joining strings with a delimiter:




CODE
String result = String.join(", ", "Java", "Python", "C++");
System.out.println(result); // "Java, Python, C++"









Mastering these common string manipulation methods in Java can significantly improve your coding efficiency and clarity. Strings are integral to most Java applications, and knowing how to manipulate them effectively will make your code more robust and maintainable. Experiment with these methods in your projects to fully harness their power!



Got any favorite string methods we missed? Let us know in the comments below!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Text Watermarking in Python: Catch Whoever Copies Your Writing
1 Quelle
Why Most Multi-Agent Systems Fail Even When Evaluation Passes
1 Quelle
A Beginner’s Guide to World Models
Modul-Fehler (similar): Das Modul konnte nicht geladen werden.