Skip to main content

Command Palette

Search for a command to run...

9) Strings in Java

Fundamental knowledge to effectively interact with strings in Java

Updated
•3 min read•View as Markdown
9) Strings in Java
A

Product-oriented Software Engineer with a solid understanding of web programming fundamentals and software development methodologies such as agile and scrum.

A String in Java represents a sequence of characters. Strings are objects, and the String class provides many methods for string manipulation.

Creating Strings

Using String Literals

String message = "Hello, World!";

Using the new Keyword

String greeting = new String("Hello");

Common String Methods

length()

The length() method returns the number of characters in a string.

String text = "Java Programming";
System.out.println("Length: " + text.length());  // Output: 16

concat()

The concat() method joins two strings.

String first = "Hello, ";
String second = "World!";

String result = first.concat(second);
System.out.println(result);  // Output: Hello, World!

equals()

The equals() method compares two strings for equality.

String str1 = "Java";
String str2 = "java";

boolean isEqual = str1.equals(str2);  // Case-sensitive
System.out.println(isEqual);  // Output: false

To perform a case-insensitive comparison, use equalsIgnoreCase().

boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
System.out.println(isEqualIgnoreCase);  // Output: true

indexOf()

The indexOf() method returns the index of the first occurrence of a character or substring.

String text = "Programming in Java";

System.out.println(text.indexOf('a'));       // Output: 8
System.out.println(text.indexOf("Java"));    // Output: 16

charAt()

The charAt() method returns the character at a specified index.

String word = "Hello";
System.out.println(word.charAt(1));  // Output: e

substring()

The substring() method returns a part of the string.

Extracting from a starting index

String text = "Java Programming";
System.out.println(text.substring(5));  // Output: Programming

Extracting a range

System.out.println(text.substring(5, 8));  // Output: Pro

toUpperCase() and toLowerCase()

These methods convert strings to uppercase or lowercase.

String text = "Hello, Java!";

System.out.println(text.toUpperCase());  // Output: HELLO, JAVA!
System.out.println(text.toLowerCase());  // Output: hello, java!

Combining Methods

String original = "  Java Programming  ";
String trimmed = original.trim();  // Removes leading and trailing spaces

System.out.println("Length: " + trimmed.length());           // Output: 16
System.out.println(trimmed.concat(" is fun!"));              // Output: Java Programming is fun!
System.out.println(trimmed.equals("Java Programming"));      // Output: true
System.out.println("Index of 'P': " + trimmed.indexOf('P')); // Output: 5
System.out.println("Char at 5: " + trimmed.charAt(5));       // Output: P
System.out.println("Substring: " + trimmed.substring(5, 11)); // Output: Progra

System.out.println(trimmed.toUpperCase());                   // Output: JAVA PROGRAMMING
System.out.println(trimmed.toLowerCase());                   // Output: java programming

Summary

  • length() returns the number of characters in a string.

  • concat() concatenates two strings.

  • equals() compares two strings for equality (case-sensitive).

  • indexOf() returns the index of the first occurrence of a character.

  • charAt() returns the character at a specific index.

  • substring() extracts a substring from the string.

  • toUpperCase() converts all characters to uppercase.

  • toLowerCase() converts all characters to lowercase.

Learning Java

Part 10 of 26

In this series, I will break down the task of learning java into smaller actionable steps and work on them topic by topic while trying to provide observability into my learning process.

Up next

10) Access, Encapsulation, and Scope in Java

Good practices of abstraction in Java