Java How to Remove the First Character from a String.

Java How to Remove the First Character from a String.

Learn How to Remove the First Character From a String.

You need to remove the first character from a string in Java. How would you go about doing this?

Using Substring

The first way we can do this is by using the substring method:

// Create a new String.
 String name = "Bob";
 
 // Remove the first character.
 name = name.substring(1);

 // Printout the string.
 System.out.println(name);

This will printout:

ob

Using String Builder

The second way we can remove the first character of a string is to use the StringBuilder's deleteCharAt method:

       // Create a new string.
        String name = "Bob";

        // Create a new StringBuilder and passing the name string.
        StringBuilder sb = new StringBuilder(name);

        // Delete the character at index zero.
        sb.deleteCharAt(0);

        // Get the new string.
        name = sb.toString();
        
        // Printout the string.
        System.out.println(name);

This will printout:

ob

Did you like it? Why don't you try also...

Change the Server Port that a Spring Boot App is running on

Change the Server Port that a Spring Boot App is running on

Learn how to change the server port that a spring boot application is running on.

Fill Java Array With The Same Values

Fill Java Array With The Same Values

Learn How To Fill a Java Array With All The Same Values.

Java How to Remove the Last Character from a String.

Java How to Remove the Last Character from a String.

Learn How to Remove the Last Character From a String.