字符串内插-重复 [英] String interpolation - repeat

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

问题描述

有没有办法在不重复的情况下多次插补变量?

例如:

var name = "bla";
Console.WriteLine($"foo {name:repeat:2} bar")

要打印

foo blabla bar

我特别感兴趣的是插入几个换行符,而不是像这样在插入掩码中重复{Environment.NewLine}几次:

$"{Environment.NewLine}{Environment.NewLine}"

推荐答案

public static string Repeat(this string s, int times, string separator = "")
{
    return string.Join(separator, Enumerable.Repeat(s, times));
}

然后使用:

Console.WriteLine($"foo {name.Repeat(2)} bar")

这篇关于字符串内插-重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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