string.insert多个值.这可能吗? [英] string.insert multiple values. Is this possible?

查看:76
本文介绍了string.insert多个值.这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在用C#学习,有一件事我似乎真的找不到答案.

如果我有一个看起来像这样的字符串"abcdefg012345",并且我想使其看起来像"ab-cde-fg-012345"

我很喜欢这样的事情:

  string S1 = "abcdefg012345";
            string S2 = S1.Insert(2, "-");
            string S3 = S2.Insert(6, "-");
            string S4 = S3.Insert.....
                ...
                ..

现在我正在寻找是否有可能以某种方式将此al变成1行,而不必制作所有这些字符串.

我认为这可能会实现吗?

解决方案

由于String的"http://en.wikipedia.org/wiki/Immutable_object" rel ="nofollow">不变性

如果您想在不创建多个字符串的情况下更有效地执行此操作,则可以使用StringBuilder.扩展方法也可能使它更易于使用.

public static class StringExtensions
{
    public static string MultiInsert(this string str, string insertChar, params int[] positions)
    {
        StringBuilder sb = new StringBuilder(str.Length + (positions.Length*insertChar.Length));
        var posLookup = new HashSet<int>(positions);
        for(int i=0;i<str.Length;i++)
        {
            sb.Append(str[i]);
            if(posLookup.Contains(i))
                sb.Append(insertChar);

        }
        return sb.ToString();

    }
}

请注意,此示例将StringBuilder初始化为正确的长度,因此避免了增长StringBuilder的需要.

用法:"abcdefg012345".MultiInsert("-",2,5); // yields "abc-def-g012345"

实时示例: http://rextester.com/EZPQ89741

Im still learning in C#, and there is one thing i cant really seem to find the answer to.

If i have a string that looks like this "abcdefg012345", and i want to make it look like "ab-cde-fg-012345"

i tought of something like this:

  string S1 = "abcdefg012345";
            string S2 = S1.Insert(2, "-");
            string S3 = S2.Insert(6, "-");
            string S4 = S3.Insert.....
                ...
                ..

Now i was looking if it would be possible to get this al into 1 line somehow, without having to make all those strings.

I assume this would be possible somehow ?

解决方案

Whether or not you can make this a one-liner (you can), it will always cause multiple strings to be created, due to the immutability of the String in .NET

If you want to do this somewhat efficiently, without creating multiple strings, you could use a StringBuilder. An extension method could also be useful to make it easier to use.

public static class StringExtensions
{
    public static string MultiInsert(this string str, string insertChar, params int[] positions)
    {
        StringBuilder sb = new StringBuilder(str.Length + (positions.Length*insertChar.Length));
        var posLookup = new HashSet<int>(positions);
        for(int i=0;i<str.Length;i++)
        {
            sb.Append(str[i]);
            if(posLookup.Contains(i))
                sb.Append(insertChar);

        }
        return sb.ToString();

    }
}

Note that this example initialises StringBuilder to the correct length up-front, therefore avoiding the need to grow the StringBuilder.

Usage: "abcdefg012345".MultiInsert("-",2,5); // yields "abc-def-g012345"

Live example: http://rextester.com/EZPQ89741

这篇关于string.insert多个值.这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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