在java中重复String的简单方法 [英] Simple way to repeat a String in java

查看:195
本文介绍了在java中重复String的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的公共方法或运算符,它允许我重复一些String n 次。我知道我可以使用for循环编写这个,但我希望在必要时避免使用循环,并且某处应该存在一个简单的直接方法。

  String str =abc; 
String repeated = str.repeat(3);

repeated.equals(abcabcabc);

相关:



重复字符串javascript
通过重复给定次数的另一个字符串来创建NSString



已修改



我尝试避免因为不完全必要而导致循环,因为:


  1. 它们会增加代码行数,即使它们隐藏在另一个函数中也是如此。


  2. 有人在阅读我的代码时必须弄清楚我在循环中做了什么。即使它被评论并且具有有意义的变量名称,它们仍然必须确保它没有做任何聪明的事情。


  3. 程序员喜欢把聪明的东西放在一起在for循环中,即使我把它写成只做它想要做的事情,这并不排除有人出现并添加一些额外的聪明修复。


  4. 他们经常容易出错。对于涉及索引的循环,往往会产生一个错误。


  5. For循环经常重用相同的变量,增加了很难找到范围错误的机会。 / p>


  6. For循环增加了一个bug猎人必须看的地方数。



'的div类= h2_lin>解决方案

从Java 11上 >有一个方法 String :: repeat ,它完全符合您的要求:

  String str =abc; 
String repeated = str.repeat(3);
repeated.equals(abcabcabc);

它的Javadoc 说:

  / ** 
*返回一个字符串,其值为此
*字符串重复{@code count}次的串联。
*< p>
*如果此字符串为空或count为零,则返回空的
*字符串。
*
* @param计算重复次数
*
* @return由此字符串组成的字符串重复
* {@code count}次或者如果
*字符串为空或count为零,则为空字符串
*
* @throws IllegalArgumentException如果{@code count}为
*为负数。
*
* @since 11
* /


I'm looking for a simple commons method or operator that allows me to repeat some String n times. I know I could write this using a for loop, but I wish to avoid for loops whenever necessary and a simple direct method should exist somewhere.

String str = "abc";
String repeated = str.repeat(3);

repeated.equals("abcabcabc");

Related to:

repeat string javascript Create NSString by repeating another string a given number of times

Edited

I try to avoid for loops when they are not completely necessary because:

  1. They add to the number of lines of code even if they are tucked away in another function.

  2. Someone reading my code has to figure out what I am doing in that for loop. Even if it is commented and has meaningful variables names, they still have to make sure it is not doing anything "clever".

  3. Programmers love to put clever things in for loops, even if I write it to "only do what it is intended to do", that does not preclude someone coming along and adding some additional clever "fix".

  4. They are very often easy to get wrong. For loops involving indexes tend to generate off by one bugs.

  5. For loops often reuse the same variables, increasing the chance of really hard to find scoping bugs.

  6. For loops increase the number of places a bug hunter has to look.

解决方案

From Java 11 on, there's a method String::repeat that does exactly what you asked for:

String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");

Its Javadoc says:

/**
 * Returns a string whose value is the concatenation of this
 * string repeated {@code count} times.
 * <p>
 * If this string is empty or count is zero then the empty
 * string is returned.
 *
 * @param count number of times to repeat
 *
 * @return A string composed of this string repeated
 * {@code count} times or the empty string if this
 * string is empty or count is zero
 *
 * @throws IllegalArgumentException if the {@code count} is
 * negative.
 *
 * @since 11
 */ 

这篇关于在java中重复String的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆