0

Good day. I would like to ask if it is possible to concatenate 2 strings to get another variable.

Lets say I have this code:

string num1 = "abcdefgh";
string num2 = "ijklmnop";
int numLength = 0;

And I want to get the value of both num1 and num2 using a forloop

for(int i =1; i<= 2; i++)
{
    numLength = ("num" + i).Length + numLength;
}
Console.WriteLine("Length is {0}", numLength);

I want it to output

Length is 16

I did the above code but it actually gives me different value.

Edit1: (P.S. I will be using more than 10 variables, I just indicated 2 of it to make it simple)

Edit2: Yes, yes. I want ("num"+i).Length to give me num1.Legnth + num2.Length.

5
  • Why do you need a for loop? Shirely you need numLength = num1.Length + num2.Length; Commented May 4, 2014 at 13:18
  • ("num" + 1) is your problem, put the strings in an array of strings or as Aron says, but I suspect you're adding more than 2 Commented May 4, 2014 at 13:18
  • You declared num1 and num2 but you never used them. What do you achieve exactly? Commented May 4, 2014 at 13:21
  • @Aron I will be using more than 10 variables so doing that style will take some time. I just posted 2 variables to make it simpler. I'll edit it anyway, to inform about this. Thanks! Commented May 4, 2014 at 13:30
  • @SonerGönül I need them for the length of that variable. (length of num1 and num2) Commented May 4, 2014 at 13:31

2 Answers 2

4

First way:

I suggest you to add all of your strings into the List and then get the total length with Sum method.

List<string> allStrings = new List<string>();
allStrings.Add(num1);
allStrings.Add(num2);
...
allStrings.Add(num10);

var totalLength = allStrings.Sum(x => x.Length);

Second way

Or if you want to calculate total length with for loop:

int totalLength = 0;
for (int i = 0; i < allStrings.Count; i++)
{
    totalLength = totalLength + allStrings[i].Length;
}

Third way

If you don't want to use List, then you can use String.Concat and then Length property.

var totalLength = String.Concat(num1, num2).Length;

The result is 16 in your case.


Edit:

In my opinion you think that, ("num" + i).Length will give you num1.Length and num2.Length. This is wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, yes. I want ("num"+i).Length to give me num1.Legnth + num2.Length.
@PaZa: Use the List<string> instead. Then it's a case of allStrings[i-1].Length (or if you do your loop from for (int i = 0; i < allStrings.Count; i++) it'll be just allStrings[i].Length without the -1)
0

Lets say we have some strings and we want the total length for all this strings.

In this case you need to store all strings in an array, so you can counter them and use indexes.

and after that a simple for (or foreach) loop can solve the problem:

            string[] texts = new string[20]; //You can use any number u need, but in my code I wrote 20.
            texts[0] = "sample text 1";
            texts[1] = "sample text 2";
            // add your strings ...

            int totalLenght = 0;
            foreach (string t in texts)
            {
                totalLenght += t.Length;
            }
            Console.WriteLine("Length is {0}", totalLenght);

If you need a variable with unlimited size, use List<T>

here is example:

            List<string> texts = new List<string>();
            texts.Add("sample text 1");
            texts.Add("sample text 2");
            // add your strings ....

            int totalLenght = 0;
            for (int i = 0; i < texts.Count; i++)
            {
                totalLenght += texts[i].Length;
            }
            Console.WriteLine("Length is {0}", totalLenght);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.