Daniel Ethereal
Daniel Ethereal

Reputation: 95

C# Concatenate strings or array of chars

I'm facing a problem while developing an application. Basically, I have a fixed string, let's say "IHaveADream"

I now want to user to insert another string, for my purpose of a fixed length, and then concatenate every character of the fixed string with every character of the string inserted by the user. e.g. The user inserts "ByeBye" then the output would be: "IBHyaevBeyAeDream".

How to accomplish this?

I have tried with String.Concat and String.Join, inside a for statement, with no luck.

Upvotes: 4

Views: 7772

Answers (4)

Fabjan
Fabjan

Reputation: 13676

If you don't care much about memory usage or perfomance you can just use:

    public static string concatStrings(string value, string value2)
    {
        string result = "";
        int i = 0;

        for (i = 0; i < Math.Max(value.Length, value2.Length) ; i++)
        {
            if (i < value.Length) result += value[i].ToString();
            if (i < value2.Length) result += value2[i].ToString();
        }

        return result;
    }

Usage:

        string conststr = "IHaveADream";
        string input = "ByeBye";
        var result = ConcatStrings(conststr, input);

        Console.WriteLine(result);

Output: IBHyaevBeyAeDream

P.S. Just checked perfomance of both methods (with strBuilder and simple cancatenation) and it appears to be that both of this methods take same time to execute (if you have just one operation). The main reason for it is that string builder take considerable time to initialize while with use of concatenation we don't need that.

But in case if you have to process something like 1500 strings then it's different story and string builder is more of an option.

For 100 000 method executions it showed 85 (str buld) vs 22 (concat) ms respectively.

My Code

Upvotes: 0

David L
David L

Reputation: 33833

One memory-efficient option is to use a string builder, since both the original string and the user input could potentially be rather large. As mentioned by Kris, you can initialize your StringBuilder capacity to the combined length of both strings.

void Main()
{
    var start = "IHaveADream";
    var input = "ByeBye";

    var sb = new StringBuilder(start.Length + input.Length);

    for (int i = 0; i < start.Length; i++)
    {
        sb.Append(start[i]);

        if (input.Length >= i + 1)
            sb.Append(input[i]);
    }

    sb.ToString().Dump();
}

This only safely accounts for the input string being shorter or equal in length to the starting string. If you had a longer input string, you'd want to take the longer length as the end point for your for loop iteration and check that each array index is not out of bounds.

void Main()
{
    var start = "IHaveADream";
    var input = "ByeByeByeByeBye";

    var sb = new StringBuilder(start.Length + input.Length);

    var length = start.Length >= input.Length ? start.Length : input.Length;
    for (int i = 0; i < length; i++)
    {
        if (start.Length >= i + 1)
            sb.Append(start[i]);

        if (input.Length >= i + 1)
            sb.Append(input[i]);
    }

    sb.ToString().Dump();
}

Upvotes: 6

Ankit Vijay
Ankit Vijay

Reputation: 4108

var firstString = "Ihaveadream";
var secondString = "ByeBye";
var stringBuilder = new StringBuilder();
for (int i = 0; i< firstString.Length; i++) {
           stringBuilder .Append(str[i]);
           if (i < secondString.Length) {
           stringBuilder .Append(secondStr[i]);
           }
}

var result = stringBuilder.ToString();

Upvotes: 0

nalyd88
nalyd88

Reputation: 5138

You can create an array of characters and then re-combine them in the order you want.

char[] chars1 = "IHaveADream".ToCharArray();
char[] chars2 = "ByeBye".ToCharArray();

// you can create a custom algorithm to handle 
// different size strings.
char[] c = new char[17];
c[0] = chars1[0];
c[1] = chars2[0];
...
c[13] = chars1[10];

string s = new string(c);

Upvotes: 0

Related Questions