luke
luke

Reputation: 13

How can I display all values of an array in C#

I am new at C#, so sorry for the basic question :)

I have a text box, in this text box i write an input.

And then I want an output with only the uneven number position of the string.

So I wrote a for loop...

string strVariable = txt.Text;

            for (int c = 0; c > strVariable.Length; c++)
            {
                string[] Array = new string[strVariable.Length];                    
                Array [c] = strVariable.Substring(c, 1);   
            }

But how can I now enter all the values of the Array in one string?

So for example I have the word "Test" in the strVariable string Then in the Array string I have "Ts" but how can I output "Ts"

Upvotes: 0

Views: 851

Answers (2)

Abdessabour Mtk
Abdessabour Mtk

Reputation: 3888

I think you should use a simple for loop without using an array like in the code below :

        string result;
        for (int c = 0; c < txt.text.Length; c++)
        {
            result += txt.text.Substring(c, 1);   
        }

Upvotes: 1

David
David

Reputation: 219057

There are undoubtedly many ways to collapse an array of strings into a single string. One such way would be to use string.Concat():

var result = string.Concat(myArray);

Note that I rename your variable to myArray here. For starters, you'll want to follow language conventions with variable names which in this case specify that the first letter should be lowercase. But more importantly, you definitely don't want to name a variable with the same name as a class in a namespace that you're using. That would cause untold confusion.

Upvotes: 3

Related Questions