我可以在 Java 中乘以字符串来重复序列吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2255500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I multiply strings in Java to repeat sequences?
提问by Mithrax
I have something like the following:
我有如下内容:
int i = 3;
String someNum = "123";
I'd like to append i
"0"s to the someNum
string. Does it have some way I can multiply a string to repeat it like Python does?
我想i
在someNum
字符串后附加“0” 。它有什么方法可以像 Python 一样乘以一个字符串来重复它吗?
So I could just go:
所以我可以去:
someNum = sumNum + ("0" * 3);
or something similar?
或类似的东西?
Where, in this case, my final result would be:
在这种情况下,我的最终结果是:
"123000".
“123000”。
回答by les2
No, but you can in Scala! (And then compile that and run it using any Java implementation!!!!)
不,但你可以在 Scala 中!(然后编译它并使用任何 Java 实现运行它!!!!)
Now, if you want to do it the easy way in java, use the Apache commons-lang package. Assuming you're using maven, add this dependency to your pom.xml:
现在,如果您想在 Java 中以简单的方式进行操作,请使用 Apache commons-lang 包。假设您使用的是 maven,请将此依赖项添加到您的 pom.xml:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
And then use StringUtils.repeat as follows:
然后使用 StringUtils.repeat 如下:
import org.apache.commons.lang.StringUtils
...
someNum = sumNum + StringUtils.repeat("0", 3);
回答by Geo
No. Java does not have this feature. You'd have to create your String using a StringBuilder, and a loop of some sort.
否。Java 没有此功能。您必须使用StringBuilder和某种循环来创建您的 String 。
回答by Vivin Paliath
I don't believe Java natively provides this feature, although it would be nice. I write Perl code occasionally and the x
operator in Perl comes in really handy for repeating strings!
我不相信 Java 本身就提供此功能,尽管它会很好。我偶尔会写 Perl 代码x
,Perl 中的运算符在重复字符串时非常方便!
However StringUtils
in commons-lang
providesthis feature. The method is called repeat()
. Your only other option is to build it manually using a loop.
不过StringUtils
incommons-lang
提供了这个功能。该方法称为repeat()
。您唯一的其他选择是使用循环手动构建它。
回答by royalsampler
There's no shortcut for doing this in Java like the example you gave in Python.
像您在 Python 中给出的示例那样,在 Java 中执行此操作没有捷径。
You'd have to do this:
你必须这样做:
for (;i > 0; i--) {
somenum = somenum + "0";
}
回答by BalusC
Two ways comes to mind:
想到了两种方法:
int i = 3;
String someNum = "123";
// Way 1:
char[] zeroes1 = new char[i];
Arrays.fill(zeroes1, '0');
String newNum1 = someNum + new String(zeroes1);
System.out.println(newNum1); // 123000
// Way 2:
String zeroes2 = String.format("%0" + i + "d", 0);
String newNum2 = someNum + zeroes2;
System.out.println(newNum2); // 123000
Way 2 can be shortened to:
方式2可以缩短为:
someNum += String.format("%0" + i + "d", 0);
System.out.println(someNum); // 123000
More about String#format()
is available in its API docand the one of java.util.Formatter
.
有关更多信息String#format()
可在其 API 文档和java.util.Formatter
.
回答by Carlos Blanco
No, you can't. However you can use this function to repeat a character.
不,你不能。但是,您可以使用此功能重复一个字符。
public String repeat(char c, int times){
StringBuffer b = new StringBuffer();
for(int i=0;i < times;i++){
b.append(c);
}
return b.toString();
}
Disclaimer: I typed it here. Might have mistakes.
免责声明:我在这里打字。可能有错误。
回答by Arbaaz
The simplest way is:
最简单的方法是:
String someNum = "123000";
System.out.println(someNum);
回答by Matt Ball
Google Guavaprovides another way to do this with Strings#repeat()
:
谷歌番石榴提供了另一种方法来做到这一点Strings#repeat()
:
String repeated = Strings.repeat("pete and re", 42);