How to Multiply Strings in Java

Rupam Yadav Feb 02, 2024
  1. Multiply Strings Using String().replace() in Java
  2. Multiply Strings Using the Stream API in Java
  3. Multiply Strings Using Guava in Java
How to Multiply Strings in Java

In this tutorial, we will learn how we can multiply a string using several methods and examples.

Multiply Strings Using String().replace() in Java

The first method to multiply a string is to use the replace() function of the String class. This replace method accepts two arguments; the first one is the target, which is the string that we want to be replaced, and the second one is the replacement string.

String() takes an array of char and then formats them into a string. We can notice that we are replacing \0 with 0. Our goal is to multiply 0 10 times and join it to the end of 123. \0 is called a null character, which finds the end of our string and replaces it with 0.

For the simplicity of the example, we have used a loop to see every step, but we can do it without the loop, and we will have a single string with all the 0’s multiplied.

public class Main {
  public static void main(String[] args) {
    for (int i = 0; i <= 10; i++) {
      String result = new String(new char[i]).replace("\0", "0");
      System.out.println("123" + result);
    }
  }

Output:

123
1230
12300
123000
1230000
12300000
123000000
1230000000
12300000000
123000000000
1230000000000

Multiply Strings Using the Stream API in Java

We can use the Stream API introduced in Java 8 to multiply a string and then join them at the end of the string. In the example below, Stream.generate() is called to create new streams from the supplier; in our case, the supplier is ourString. limit() is used with the Stream to limit the number of values it must return.

The reduce() method is used to perform a binary operation and then return a single value. We are adding the elements and then returning the added value. We can notice that after all the operations, we get the result1 of Optional<String>, which is returned when there is a chance that the result might be empty.

At last, we can check if the result1 is not empty by using isPresent() and then fetch the string using the get() method.

import java.util.Optional;
import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    String ourString = "123";

    for (int count = 0; count <= 10; count++) {
      Optional<String> result1 =
          Stream.generate(() -> ourString).limit(count).reduce((a, b) -> a + b);

      String finalResult;
      if (result1.isPresent()) {
        finalResult = result1.get();
        System.out.println(finalResult);
      }
    }
  }
}

Output:

123
123123
123123123
123123123123
123123123123123
123123123123123123
123123123123123123123
123123123123123123123123
123123123123123123123123123
123123123123123123123123123123

Multiply Strings Using Guava in Java

In the last method to multiply strings in Java, we will use a library called Guava. To use this library, we have to import it using the following maven dependency.

 <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.0-jre</version>
  </dependency>

Strings.repeat() method of the Guava library can be used to repeat the ourString multiple times. It takes two arguments, the string that we want to multiply and then the count of repetitions.

import com.google.common.base.Strings;

public class Main {
  public static void main(String[] args) {
    String ourString = "1234";
    for (int count = 0; count < 5; count++) {
      String finalResult = Strings.repeat(ourString, count);
      System.out.println(finalResult);
    }
  }
}

Output:

1234
12341234
123412341234
1234123412341234
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java String