Java:字符串-n次添加字符 [英] Java: String - add character n-times

查看:48
本文介绍了Java:字符串-n次添加字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法可以向现有字符串中添加n次字符或另一个字符串?我在 String Stringbuilder 等中找不到任何内容.

Is there a simple way to add a character or another String n-times to an existing String? I couldn’t find anything in String, Stringbuilder, etc.

推荐答案

您可以使用Java 8流API来执行此操作.以下代码从"c" 创建字符串"cccc" :

You are able to do this using Java 8 stream APIs. The following code creates the string "cccc" from "c":

String s = "c";
int n = 4;
String sRepeated = IntStream.range(0, n).mapToObj(i -> s).collect(Collectors.joining(""));

这篇关于Java:字符串-n次添加字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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