1 liner for String.Split

  • Thread starter Thread starter Robert
  • Start date Start date
R

Robert

in regards to parsing directories..

Dim Sep(0) as char
Sep(0) = "\"
Dim Parts() as string = Filename.Split(Sep)


What I want is to declare a temporary variable that is passed to split directly.

Tried a few ways, none work...

Any ideas?
 
Robert said:
in regards to parsing directories..

Dim Sep(0) as char
Sep(0) = "\"
Dim Parts() as string = Filename.Split(Sep)


What I want is to declare a temporary variable that is passed to split directly.

Tried a few ways, none work...

Any ideas?
I'm not really sure what you're asking, but is this what you want? -

Dim Parts() As String = Filename.Split("\"c)


ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
Robert said:
in regards to parsing directories..

Dim Sep(0) as char
Sep(0) = "\"
Dim Parts() as string = Filename.Split(Sep)


What I want is to declare a temporary variable that is passed to split
directly.

Tried a few ways, none work...

Any ideas?


I would do it this way instead:
Dim Parts() As String =
Filename.Split(System.IO.Path.DirectorySeparatorChar)
 
Dim Sep(0) as char
Sep(0) = "\"
Dim Parts() as string = Filename.Split(Sep)


What I want is to declare a temporary variable that is passed to split
directly.

Tried a few ways, none work...
If its VB, why not just use the Split function instead of the split method
of the String class.

Dim Parts() as String = split(Filename, "\")
 
Robert said:
in regards to parsing directories..

Dim Sep(0) as char
Sep(0) = "\"
Dim Parts() as string = Filename.Split(Sep)


What I want is to declare a temporary variable that is passed to split directly.

Tried a few ways, none work...

Any ideas?
Here you go:

Dim Parts() As String = Filename.Split(New Char() {"\"c})
 
Dim Sep(0) as char
Dim Parts() As String = Filename.Split(New Char() {"\"c})

Ding, Ding! We have a winner.

First, it must be a char array or a string array.
Second using the VisualBasic namespace is just not something I do ever since a large PocketPC port.
The name space does not exist there.

Third, I swear I tried the above, but without the New!

Now, why the new? Because that makes it work, DUH, got that.
but in my example above, new is not needed..

Thanks a bunch for saving some useless filler lines. Conciseness is important.
 
Robert said:
Ding, Ding! We have a winner.

First, it must be a char array or a string array.
Second using the VisualBasic namespace is just not something I do ever
since a large PocketPC port.
The name space does not exist there.

Third, I swear I tried the above, but without the New!

Now, why the new? Because that makes it work, DUH, got that.
but in my example above, new is not needed..

Thanks a bunch for saving some useless filler lines. Conciseness is
important.


OK, but calling split with a single character does work. I see that the
documentation says it must be an array. There must be a conversion to an
array as all of the single character versions posted work fine for me.
 
Family Tree Mike said:
First, it must be a char array or a string array.
Second using the VisualBasic namespace is just not something I do ever
since a large PocketPC port.
The name space does not exist there.

Third, I swear I tried the above, but without the New!
[...]
OK, but calling split with a single character does work. I see that the
documentation says it must be an array. There must be a conversion to an
array as all of the single character versions posted work fine for me.

Just use '... = s.Split("/"c)'. The parameter is actually a parameter array
('ParamArray'), which allows multiple ways of calling:

* '... = s.Split(New Char() {"a"c, "b"c})'
* '... = s.Split("a"c, "b"c)'
 
Herfried K. Wagner said:
Family Tree Mike said:
First, it must be a char array or a string array.
Second using the VisualBasic namespace is just not something I do ever since a large PocketPC port.
The name space does not exist there.

Third, I swear I tried the above, but without the New!
[...]
OK, but calling split with a single character does work. I see that the documentation says it must be an array.
There must be a conversion to an array as all of the single character versions posted work fine for me.

Just use '... = s.Split("/"c)'. The parameter is actually a parameter array ('ParamArray'), which allows multiple
ways of calling:

* '... = s.Split(New Char() {"a"c, "b"c})'
* '... = s.Split("a"c, "b"c)'


I tried at least 5 different permutations on this. None worked...

Then with the new VB with {} I poked around some more thinking, oooh declare and
initialize in one shot! Can remove some useless lines. Still nothing..

not sure where I got distracted, as it is disgustingly, obviously, no-way-to-go-wrong, dead easy!

Must be getting too old..
 
Back
Top