I want to simply write a repeating string

def repeat(str, n):
    return str * n 

If you want to generate a new string that repeats the contents of the string n times, you can write it in Python as in the example above. In other words, the * operator has the function of repeating a character string, but when it comes to realizing the same thing in Java, I think that the following methods are often defined (null check etc. abridgement).

public static String repeat(String str, int n) {
  var sb = new StringBuilder();
  while(n-- > 0) sb.append(str);
  return sb.toString();
}

It's true that this is fine, but it's a shame that you can write in one line in Python but not in Java (mystery). So I often think about how to write string repetitions simply in Java, but the first thing I personally think of is to use Stream.

public static String repeat(String str, int n) {
  return IntStream.range(0, n).mapToObj(i -> str).collect(Collectors.joining(""));
}

It's certainly one line, but I get the impression that it's written in a slightly tricky way to make it one line. In order to make it simpler, I came up with the following method while researching various things.

public static String repeat(String str, int n) {
  return String.join("", Collections.nCopies(n, str));
}

It's shorter and simpler. I don't feel like doing tricky things to make it shorter. Since Collections.nCopies generates a list, I would like to actively use it while paying attention to that.

It's not good to use Apache Commons Lang's StringUtils.repeat (´ ・ ω ・ `)

Recommended Posts

I want to simply write a repeating string
I want to write a unit test!
I want to develop a web application!
[Ruby] I want to do a method jump!
I want to design a structured exception handling
I want to write quickly from java to sqlite
I want to use a little icon in Rails
I want to define a function in Rails Console
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function
I want to convert characters ...
To write a user-oriented program (1)
[Java] I want to convert a byte array to a hexadecimal number
I want to find a relative path in a situation using Path
I want to make a specific model of ActiveRecord ReadOnly
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
I want to create a form to select the [Rails] category
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
I want to use FireBase to display a timeline like Twitter
I want to write a loop that references an index with Java 8's Stream API
Swift: I want to chain arrays
How to write a ternary operator
I want to docker-compose up Next.js!
I want to recursively search for files under a specific directory
I want to create a chat screen for the Swift chat app!
I want to make a button with a line break with link_to [Note]
I want to be able to think and write regular expressions myself. ..
I want to add a browsing function with ruby on rails
I want to use swipeback on a screen that uses XLPagerTabStrip
I just want to write Java using Eclipse on my Mac
I tried to write code like a type declaration in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
I got stuck trying to write a where in clause in ActiveRecord
I want to extract between character strings with a regular expression
A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]
[Ruby] I want to output only the odd-numbered characters in the character string
[Rails] I want to send data of different models in a form
I want to write JSP in Emacs more easily than the default.
I want to eliminate duplicate error messages
[Basic] How to write a Dockerfile Self-learning ②
I want to display background-ground-image on heroku.
I want to select multiple items with a custom layout in Dialog
I want to use DBViewer with Eclipse 2018-12! !!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to summarize Apache Wicket 8 because it is a good idea
I want to RSpec even at Jest!
[Introduction to Java] How to write a Java program
I want to install PHP 7.2 on Ubuntu 20.04.
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I want to display a PDF in Chinese (Korean) with thin reports
Notation to put a variable in a string
I want to stop Java updates altogether
I want to use @Autowired in Servlet