Counting amount of results of a Split()

Asked

Viewed 83 times

3

I have a string that looks like this:

Yeah, No, Maybe

And I have a code that I need to put these string results separately into a <option> of a <select>.

I intend to do this through a for(). but I need to count the results of my split to put there, for example, picking up this string, would be:

Yes No Maybe

the results of a string.Split(",");. But I need the number of how many results they gave, which in this case would be 3.

1: Sim
2: Não
3: Talvez

How do I count how many results I got in a split?

I’m doing on . NET and I tried to put a .Count to see if it stuck, but it didn’t... how do I do it?

1 answer

3


When you make a Split of a text of this format (Sim,Não,Talvez) will return a array simple and immutable with the amount of information generated by this condition and to know the quantity use Length, example:

string texto = "Sim,Não,Talvez";
string[] textos = texto.Split(',');
textos.Length; // 3 itens

References:

  • Hmm... worked. Just one more question. Do you know how I change your order? For example, in this case it would be Sim Não Talvez, but there was some way I could leave the order like this Talvez Não Sim?

  • It is already another question, to stir up the order needs to know what is the condition you stipulated for this to happen, you want the array in reverse ??? what is the rule

  • yes.. the array in reverse only

  • makes it from the largest to the smallest, example for(int i = textos.Length - 1; i >= 0;i--){}

  • 1

    or use Array.Reverse(textos) and print normally ... I will not put in the answer because it is not part of the question ...

  • Thank you, you’ll help me.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.