none
Is there any way to determine the number of arrays that a "Split" creates? RRS feed

  • Question

  • When I do a "Split" based on a character, sometime my data will have [0],[1], and [2]. Sometimes only [1]. Sometimes only [0]. How can I account for this? Is there a way for me to determine the number of subscripts, array, index number, that is dynamically created by a "Split" so that I can string out my data and store it appropriately?

    Thanks for your review and am hopeful for a reply.

    PSULionRP

    Thursday, December 5, 2013 11:57 PM

Answers

  • Hi,

    If I do a split on this string "Hello,world,i'm,good", I will have 4 elements which the maximum subscript will be 3 (4 - 1). To get the length you use .Length property:

    string text = "Hello,world,i'm,good";
    int length = text.Split(',').Length;
    

    Note that length is 4 not 3. 4 is the number of elements and 3 (length - 1) is the maximum subscript.

    So your array will end at text[3]

    • Proposed as answer by Jason Dot Wang Tuesday, December 10, 2013 8:43 AM
    • Marked as answer by Jason Dot Wang Friday, December 13, 2013 3:18 AM
    Friday, December 6, 2013 6:04 AM

All replies

  • You should be able to use the .Length property of the resulting array from the Split.

    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    • Proposed as answer by chriga Sunday, December 8, 2013 12:51 AM
    Friday, December 6, 2013 12:17 AM
  • Can you show some samples of the raw data?  I'm not quite sure what you mean by

    "Sometimes only [1]. Sometimes only [0]. "

    .Length will tell you how many entries in an array, as mentioned, but if the raw data just randomly omits some fields then you have a more difficult problem.


    Paul Linton

    Friday, December 6, 2013 12:31 AM
  • Hi,

    If I do a split on this string "Hello,world,i'm,good", I will have 4 elements which the maximum subscript will be 3 (4 - 1). To get the length you use .Length property:

    string text = "Hello,world,i'm,good";
    int length = text.Split(',').Length;
    

    Note that length is 4 not 3. 4 is the number of elements and 3 (length - 1) is the maximum subscript.

    So your array will end at text[3]

    • Proposed as answer by Jason Dot Wang Tuesday, December 10, 2013 8:43 AM
    • Marked as answer by Jason Dot Wang Friday, December 13, 2013 3:18 AM
    Friday, December 6, 2013 6:04 AM