Need generic enumerator when calling string.split()

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.

string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);

// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<string> tokensEnum =
(System.Collections.Generic.IEnumerator<string>)tokens.GetEnumerator();

// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}

the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."
 
Dave said:
I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.

string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);

// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<string> tokensEnum =
(System.Collections.Generic.IEnumerator<string>)tokens.GetEnumerator();

// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}

the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."

How about this:

IEnumerable<string> tokens = str.Split(delimiterChars);

IEnumerator<string> tokensEnum = tokens.GetEnumerator();
 
Dave said:
I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.
string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);
// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<string> tokensEnum =
(System.Collections.Generic.IEnumerator<string>)tokens.GetEnumerator();
// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}
the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."

How about this:

IEnumerable<string> tokens = str.Split(delimiterChars);

IEnumerator<string> tokensEnum = tokens.GetEnumerator();

--
Jon Skeet - <[email protected]>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too- Hide quoted text -

- Show quoted text -

Perhaps close but no, this does not seem to work.

tokens is not broken up into buckets as it is when receiving
string.Split() into string[] array.
I.e. The tokens enumerator has same value as original string "abc:
value1 def: value2 ghi: value3" with only 1 dimension (rather
than 3).
 
Dave said:
I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.
string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);
// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<string> tokensEnum =
(System.Collections.Generic.IEnumerator<string>)tokens.GetEnumerator();
// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}
the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."
How about this:
IEnumerable<string> tokens = str.Split(delimiterChars);
IEnumerator<string> tokensEnum = tokens.GetEnumerator();
- Show quoted text -

Perhaps close but no, this does not seem to work.

tokens is not broken up into buckets as it is when receiving
string.Split() into string[] array.
I.e. The tokens enumerator has same value as original string "abc:
value1 def: value2 ghi: value3" with only 1 dimension (rather
than 3).- Hide quoted text -

- Show quoted text -

oops oops, your idea works fine!

i was having problems with my iterator so case closed. thanks for
your help.
 
Back
Top