是否有一种简单的方法来返回重复X次的字符串?

本文翻译自:Is there an easy way to return a string repeated X number of times?

I'm trying to insert a certain number of indentations before a string based on an items depth and I'm wondering if there is a way to return a string repeated X times. 我试图根据项目深度在字符串之前插入一定数量的缩进,并且我想知道是否有办法返回重复X次的字符串。 Example: 例:

string indent = "---";
Console.WriteLine(indent.Repeat(0)); //would print nothing.
Console.WriteLine(indent.Repeat(1)); //would print "---".
Console.WriteLine(indent.Repeat(2)); //would print "------".
Console.WriteLine(indent.Repeat(3)); //would print "---------".

#1楼

参考:https://stackoom.com/question/fKJM/是否有一种简单的方法来返回重复X次的字符串


#2楼

Another approach is to consider string as IEnumerable<char> and have a generic extension method which will multiply the items in a collection by the specified factor. 另一种方法是将string视为IEnumerable<char>并具有通用的扩展方法,该方法会将集合中的项目乘以指定的因子。

public static IEnumerable<T> Repeat<T>(this IEnumerable<T> source, int times)
{
    source = source.ToArray();
    return Enumerable.Range(0, times).SelectMany(_ => source);
}

So in your case: 因此,在您的情况下:

string indent = "---";
var f = string.Concat(indent.Repeat(0)); //.NET 4 required
//or
var g = new string(indent.Repeat(5).ToArray());

#3楼

Surprised nobody went old-school. 惊讶的是没人上过老派。 I am not making any claims about this code, but just for fun: 我对此代码没有任何要求,只是为了好玩:

public static string Repeat(this string @this, int count)
{
    var dest = new char[@this.Length * count];
    for (int i = 0; i < dest.Length; i += 1)
    {
        dest[i] = @this[i % @this.Length];
    }
    return new string(dest);
}

#4楼

Use String.PadLeft , if your desired string contains only a single char. 如果所需的字符串仅包含一个字符,请使用String.PadLeft

public static string Indent(int count, char pad)
{
    return String.Empty.PadLeft(count, pad);
}

Credit due here 此处付款


#5楼

public static class StringExtensions
{
    public static string Repeat(this string input, int count)
    {
        if (!string.IsNullOrEmpty(input))
        {
            StringBuilder builder = new StringBuilder(input.Length * count);

            for(int i = 0; i < count; i++) builder.Append(input);

            return builder.ToString();
        }

        return string.Empty;
    }
}

#6楼

If you're using .NET 4.0, you could use string.Concat together with Enumerable.Repeat . 如果使用的是.NET 4.0,则可以将string.ConcatEnumerable.Repeat一起使用。

int N = 5; // or whatever
Console.WriteLine(string.Concat(Enumerable.Repeat(indent, N)));

Otherwise I'd go with something like Adam's answer . 否则,我会接受亚当的回答

The reason I generally wouldn't advise using Andrey's answer is simply that the ToArray() call introduces superfluous overhead that is avoided with the StringBuilder approach suggested by Adam. 我通常建议使用Andrey的答案的原因仅仅是ToArray()调用引入了多余的开销,而Adam提出的StringBuilder方法可以避免这种开销。 That said, at least it works without requiring .NET 4.0; 就是说,至少它不需要.NET 4.0就可以工作。 and it's quick and easy (and isn't going to kill you if efficiency isn't too much of a concern). 而且它既快捷又容易(如果效率不是您所关注的重点,那也不会杀死您)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值