Java StringBuilder length() Method

The StringBuilder.length() method in Java is used to obtain the number of characters currently in a StringBuilder object.

Table of Contents

  1. Introduction
  2. length Method Syntax
  3. Examples
    • Retrieving the Length of a StringBuilder
    • Length After Modifications
  4. Real-World Use Case
  5. Conclusion

Introduction

The StringBuilder.length() method is part of the StringBuilder class in Java. It provides a way to get the current length (number of characters) of the character sequence contained in the StringBuilder object. This method is particularly useful when you need to know the size of the content in the StringBuilder.

length() Method Syntax

The syntax for the length method is as follows:

public int length()

This method does not take any parameters and returns an integer representing the number of characters in the StringBuilder.

Examples

Retrieving the Length of a StringBuilder

The length method can be used to get the number of characters currently in a StringBuilder.

Example

public class StringBuilderLengthExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        // Get the length of the StringBuilder
        int length = sb.length();

        // Print the length
        System.out.println("Length of StringBuilder: " + length);
    }
}

Output:

Length of StringBuilder: 13

Length After Modifications

You can check the length of a StringBuilder before and after making modifications to see how the length changes.

Example

public class StringBuilderLengthExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");

        // Print the initial length
        System.out.println("Initial length: " + sb.length());

        // Append a string
        sb.append(", World!");
        System.out.println("Length after append: " + sb.length());

        // Delete a portion of the string
        sb.delete(5, 7);
        System.out.println("Length after delete: " + sb.length());
    }
}

Output:

Initial length: 5
Length after append: 13
Length after delete: 11

In this example, the initial length of the StringBuilder is 5. After appending “, World!”, the length increases to 13. After deleting the characters between indices 5 and 7, the length decreases to 11.

Real-World Use Case

Example: Validating Input Length

In a real-world scenario, you might need to validate the length of user input before processing it. Using the StringBuilder.length() method, you can check if the input meets the required length constraints.

Example Code

public class InputValidator {
    public static void main(String[] args) {
        StringBuilder userInput = new StringBuilder("JohnDoe123");

        // Check if the input length is within the valid range (5 to 15 characters)
        if (isValidLength(userInput)) {
            System.out.println("Input is valid.");
        } else {
            System.out.println("Input is invalid. Length should be between 5 and 15 characters.");
        }
    }

    public static boolean isValidLength(StringBuilder input) {
        int length = input.length();
        return length >= 5 && length <= 15;
    }
}

Output:

Input is valid.

Conclusion

The StringBuilder.length() method in Java is used for determining the number of characters in a StringBuilder object. By understanding how to use this method, you can efficiently manage and validate the content of your StringBuilder. Whether you need to retrieve the current length, check the length after modifications, or validate input length, the length method provides a reliable solution for these tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top