String.insert multiple values. Is it possible?

I am still involved in C #, and there is one thing that I really cannot find the answer to.

If I have a line that looks like this: "abcdefg012345" and I want it to look like "ab-cde-fg-012345"

i something like this:

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

Now I was looking to see if it is possible to get this al in 1 line, without having to do all of these lines.

I guess this is possible somehow?

+2
source share
8 answers

Is it possible to do this in one layer (you can), it will always create multiple lines due to immutability String in .NET

, , 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

+6
string S1 = "abcdefg012345".Insert(2, "-").Insert(6, "-")..... ;
+3

, string.Format(). :

string strTarget = String.Format("abc{0}def{0}g012345","-");
+2
string s = "abcdefg012345";
foreach (var index in [2, 6, ...]
{
    s = s.Insert(index, "-");
}
+1

StringBuilder , Strings objects immutable, . http://msdn.microsoft.com/en-us/library/2839d5h5(v=vs.71).aspx

: http://www.dotnetperls.com/stringbuilder

:

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("abcdefg012345");
            sb.Insert(2, '-');
            sb.Insert(6, '-');

            Console.WriteLine(sb);
            Console.Read();
        }
    }
}

, - :

StringBuilder sb = new StringBuilder("abcdefg012345").Insert(2, '-').Insert(6, '-');
0

    StringBuilder sb = new StringBuilder("abcdefg012345");
    sb.Insert(6, '-').Insert(2, '-').ToString();
0
        String s1 = "abcdefg012345";
        String seperator = "-";
        s1 = s1.Insert(2, seperator).Insert(6, seperator).Insert(9, seperator);

. , Insert s1 , Insert ..

, String , , , . , String , , , .

0

Aggregate, :

string result = new[] { 2, 5, 8, 15 }.Aggregate("abcdefg012345", (s, i) => s.Insert(i, "-"));

result - ab-cd-ef-g01234-5. . .

Edit: this solution is not valid, because "-" will be inserted into the index of the already modified row, and not at the position on the original row. But then again, most of the answers here suffer from the same problem.

0
source

Source: https://habr.com/ru/post/1671797/


All Articles