Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Strings in Java

String Methods in Java

Let's explore some important String Methods in Java. By leveraging these concepts and methods, you can write optimized, reliable, and maintainable code when working with strings.

contains()

The Java contains() method searches for a sequence of characters in a string. It returns true if the sequence exists; otherwise, it returns false.

Syntax:

string_name.contains("sequence");

Returns: true if the sequence exists, otherwise false. Throws: NullPointerException if the sequence is null.

endsWith()

The Java endsWith() method checks if a string ends with the given suffix.

Syntax:

string_name.endsWith("sequence or character");

Returns: true or false.

indexOf()

The Java indexOf() method returns the index of a given character or substring. If not found, it returns -1.

Common Variants:

  1. int indexOf(int ch): Returns index of given character.

  2. int indexOf(int ch, int fromIndex): Returns index of given character starting from fromIndex.

  3. int indexOf(String substring): Returns index of given substring.

  4. int indexOf(String substring, int fromIndex): Returns index of given substring starting from fromIndex.

charAt()

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

Syntax:

char c = string_name.charAt(index);

Returns: Character at the specified index. Throws: StringIndexOutOfBoundsException if index is out of range.

startsWith()

The Java startsWith() method checks if a string starts with a given prefix.

Syntax:

public boolean startsWith(String prefix);
public boolean startsWith(String prefix, int offset);

Returns: true or false.

substring()

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

Syntax:

public String substring(int startIndex);
public String substring(int startIndex, int endIndex);

Returns: Extracted substring. Throws: StringIndexOutOfBoundsException if indices are invalid.

toLowerCase()

The Java toLowerCase() method converts all characters in a string to lowercase.

Syntax:

public String toLowerCase();
public String toLowerCase(Locale locale);

Returns: Lowercase string.

toUpperCase()

The Java toUpperCase() method converts all characters in a string to uppercase.

Syntax:

public String toUpperCase();
public String toUpperCase(Locale locale);

Returns: Uppercase string.

Understanding == Operator

The == operator checks whether two string variables point to the same memory location. If two strings have the same content but are stored in different memory locations, == will return false.

Example:

String s1 = "java";
String s2 = "java";
String s3 = new String("java");

System.out.println(s1 == s2); // true (same reference from String pool)
System.out.println(s1 == s3); // false (different memory locations)

By understanding these concepts, you will be able to efficiently compare and work with strings in Java.

String Concatenation in Java

String concatenation in Java creates a new string by combining multiple strings. There are two ways to concatenate strings in Java:

  1. Using + (string concatenation) operator:

    String s1 = "Hello";
    String s2 = " World";
    String s3 = s1 + s2;
    System.out.println(s3); // Output: Hello World
  2. Using concat() method:

    String s1 = "Hello";
    String s2 = " World";
    String s3 = s1.concat(s2);
    System.out.println(s3); // Output: Hello World

By understanding these concepts, you will be able to efficiently compare, modify, and concatenate strings in Java.

Summary

Understanding string comparison methods like equals(), compareTo(), and == helps developers avoid common pitfalls related to reference and content comparison. Additionally, string concatenation and essential methods such as contains(), startsWith(), substring(), and toUpperCase() provide powerful ways to manipulate strings effectively.

By leveraging these concepts and methods, Java developers can write optimized, reliable, and maintainable code when working with strings.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board