Lesson Introduction

Welcome! Today, we're going to delve into the fundamental operations and methods associated with Strings in Java. Strings are the bread and butter for most programs because they're used to represent and manipulate text data. This lesson will enable you to learn about basic string methods and operations in Java such as concatenation, comparison, and most common methods.

Revising String Concatenation

We already know what concatenation is and how it works, but worth revising it quickly! Concatenation means joining things together and is a crucial string operation. In Java, we accomplish this with the + operator or using StringBuilder.

String hello = "Hello, ";
String world = "World!";
String greeting = hello + world; // "Hello, World!"

In this case, "Hello, " and "World!" are combined to form one string: "Hello, World!".

Comparing Strings

There are often times when we need to compare Strings. Unfortunately, just a common comparison operators == and < will not work here. To accomplish this in Java, we use the equals() and compareTo() methods.

The equals() method, as the name suggests, checks if two Strings are identical.

String firstWord = "Hello";
String secondWord = "Hello";
boolean areEqual = firstWord.equals(secondWord); // true
System.out.println(areEqual); // Outputs: true

Here, since firstWord and secondWord are equal, areEqual is true.

The compareTo() method, on the other hand, is used to compare two strings alphabetically. compareTo() will return 0 if the strings are equal, a negative number if the first string comes first alphabetically, and a positive number otherwise.

String firstWord = "Apple";
String secondWord = "Banana";
int comparisonResult = firstWord.compareTo(secondWord); // A negative number because 'Apple' comes before 'Banana' alphabetically.
System.out.println(firstWord + " is less than " + secondWord + "? Comparison result: " + comparisonResult);
// Apple is less than Banana? Comparison result: -1

As you can see, the comparison result is negative, meaning that "Apple" is alphabetically smaller than "Banana"

Important String Methods

Now, let's explore the other common String methods:

  • length(): This method returns the number of characters in the String.
String word = "Hello";
int length = word.length(); // 5
  • substring(int beginIndex, int endIndex): This method returns the section of the original String from beginIndex (inclusive) to endIndex (exclusive).
String sentence = "Hello, World!";
String word = sentence.substring(0, 5); // "Hello"
  • toLowerCase() and toUpperCase(): These methods return the string in either lowercase or uppercase, respectively.
String word = "Hello";
String lowerCaseWord = word.toLowerCase(); // "hello"
String upperCaseWord = word.toUpperCase(); // "HELLO"
  • trim(): This method removes all white spaces at the beginning and the end of the String.
String sentence = " Hello, World!   ";
String trimmedSentence = sentence.trim(); // "Hello, World!"
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal