1 liner for String.Split

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?
 
S

ShaneO

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.
 
F

Family Tree Mike

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)
 
H

Hal Rosser

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, "\")
 
T

Teme64

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})
 
R

Robert

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.
 
F

Family Tree Mike

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.
 
H

Herfried K. Wagner [MVP]

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)'
 
R

Robert

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..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top