Handling Strings

johnvista

Member
Joined
Jun 16, 2006
Messages
6
Programming Experience
Beginner
Hello to everyone at VB.NET Fourms.
I need some help with handling strings.

Example of what I need:
  • http://blahblahblah.com/wtf.gz|stargatetc*appone
How would I use visual basic.net 2005 to reconise the "|" and the "*" and seperate them so i end up with three strings:Your help is much appreciated :)
thanks
 
VB.NET:
        Dim ST As String = "http://blahblahblah.com/wtf.gz|stargatetc*appone"
        Dim Pos1 As Integer = ST.IndexOf("|")
        Dim Pos2 As Integer = ST.IndexOf("*")
        Dim ST1 As String = ST.Substring(0, Pos1)
        Dim ST2 As String = ST.Substring(Pos1 + 1, Pos2 - Pos1 - 1)
        Dim ST3 As String = ST.Substring(Pos2 + 1)
        Console.WriteLine(ST1 & vbCr & ST2 & vbCr & ST3)
 
VB.NET:
        Dim Str As String = "http://blahblahblah.com/wtf.gz|stargatetc*appone"
        Dim SplitStr() As String
        Str = Str.Replace("|", "*")

        SplitStr = Str.Split("*")
        MsgBox(SplitStr(0) & vbCr & SplitStr(1) & vbCr & SplitStr(2))
EDIT: Looks like i was beaten to it :p
 
You can split string by an array of delimiters like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Str [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"http://blahblahblah.com/wtf.gz|stargatetc*appone"
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] spl() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Str.Split([/SIZE][SIZE=2][COLOR=#0000ff]New [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Char[/COLOR][/SIZE][SIZE=2]() {[/SIZE][SIZE=2][COLOR=#800000]"|"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"*"[/COLOR][/SIZE][SIZE=2]})
[/SIZE][SIZE=2][COLOR=#008000]'spl(0) is now http://blahblahblah.com/wtf.gz
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'spl(1) is now stargatetc
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'spl(2) is now appone
[/COLOR][/SIZE]
By the way, the vertical bar (pipe) | is not valid in URL it must be encoded "%7c", in which case you could do this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Str [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"http://blahblahblah.com/wtf.gz%7cstargatetc*appone"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] spl() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Str.Split([/SIZE][SIZE=2][COLOR=#0000ff]New [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]() {[/SIZE][SIZE=2][COLOR=#800000]"%7c"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"*"[/COLOR][/SIZE][SIZE=2]}, StringSplitOptions.None)
[/SIZE]
 
Back
Top