Connecting Tech Pros Worldwide Forums | Help | Site Map

1 liner for String.Split

Robert
Guest
 
Posts: n/a
#1: Nov 21 '08
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?



ShaneO
Guest
 
Posts: n/a
#2: Nov 21 '08

re: 1 liner for String.Split


Robert wrote:
Quote:
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.
Family Tree Mike
Guest
 
Posts: n/a
#3: Nov 21 '08

re: 1 liner for String.Split


"Robert" <no@spam.comwrote in message
news:ODQ%23C53SJHA.5268@TK2MSFTNGP04.phx.gbl...
Quote:
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.DirectorySeparatorCh ar)

Hal Rosser
Guest
 
Posts: n/a
#4: Nov 21 '08

re: 1 liner for String.Split


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


Teme64
Guest
 
Posts: n/a
#5: Nov 21 '08

re: 1 liner for String.Split


Robert wrote:
Quote:
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})

--

Teme64 @ http://windevblog.blogspot.com
Robert
Guest
 
Posts: n/a
#6: Nov 21 '08

re: 1 liner for String.Split


>Dim Sep(0) as char
Quote:
Quote:
>Sep(0) = "\"
>Dim Parts() as string = Filename.Split(Sep)
>
>
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.


Family Tree Mike
Guest
 
Posts: n/a
#7: Nov 21 '08

re: 1 liner for String.Split


"Robert" <no@spam.comwrote in message
news:eF4M7k6SJHA.5408@TK2MSFTNGP04.phx.gbl...
Quote:
Quote:
Quote:
>>Dim Sep(0) as char
>>Sep(0) = "\"
>>Dim Parts() as string = Filename.Split(Sep)
>>
>>
>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.
>

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.

Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#8: Nov 21 '08

re: 1 liner for String.Split


"Family Tree Mike" <FamilyTreeMike@ThisOldHouse.comschrieb:
Quote:
Quote:
>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)'

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Robert
Guest
 
Posts: n/a
#9: Nov 22 '08

re: 1 liner for String.Split



"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrote in message news:eBF8jyCTJHA.1960@TK2MSFTNGP04.phx.gbl...
Quote:
"Family Tree Mike" <FamilyTreeMike@ThisOldHouse.comschrieb:
Quote:
Quote:
>>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..


Closed Thread


Similar Visual Basic .NET bytes