[Help] Splitting strings. [solved]

Posts 1–13 of 13 · Page 1 of 1
[Help] Splitting strings. [solved]
Ok, i seem to be stuck at a wall in a program im making.
what im stuck at is this:
127.0.0.1:4254
what i want to do is take the port off this string and put it into another string for use. Problem is, its got to be able to go on any IP string, not a user-set one. So its got to search for the ":" and pull anything after it off for use. First half would be nice to keep aswell.....
tryed:
[highlight=vb.net]
dim ip as string = console.readline()
dim varscip as integer = 0
ip.split(new char, (":"{c})
for each line in ip
Varscip += 1
If varscip = 2 then
console.writeline(line)
end if
next
varscip = 0
[/highlight]
Cant remember if that .split is correct, but should be close to what i used...
Quote Originally Posted by *****179 View Post
Ok, i seem to be stuck at a wall in a program im making.
what im stuck at is this:
127.0.0.1:4254
what i want to do is take the port off this string and put it into another string for use. Problem is, its got to be able to go on any IP string, not a user-set one. So its got to search for the ":" and pull anything after it off for use. First half would be nice to keep aswell.....
tryed:
dim ip as string = console.readline()
dim varscip as integer = 0
ip.split(new char, (":"{c})
for each line in ip
Varscip += 1
If varscip = 2 then
console.writeline(line)
end if
next
varscip = 0

Cant remember if that .split is correct, but should be close to what i used...
Try something like this:

[highlight=vb.net]
Dim rawIPstring As String = "127.0.0.1:4254"
Dim ipPort As String() = rawIPstring.Split(":"c)
Dim portNumber As String = ipPort(1)
Dim ipAddress As String = ipPort(0)
[/highlight]

last two variables are optional, just put them there to illustrate where the values are in the array.
Ok, test it when i can.
btw, whats the reason my code did not work?
Quote Originally Posted by *****179 View Post
Ok, test it when i can.
btw, whats the reason my code did not work?
ip.Split(New Char, (":"{c}))

What is that lol?
Thought you had to define it a bit more.....
Quote Originally Posted by *****179 View Post
Thought you had to define it a bit more.....
Look at the overloads on the Split function, I haven't opened VB in a while but I'm pretty sure there is at least the following:

separator() as char <- character array. This means that you can split between different chars
separator() as char, count as integer <- split about all the chars in the array, but only split the first <count> occurrence(s).
and a few more.

So really mine should have been:

[highlight=vb.net]
Dim ipPort As String() = rawIPstring.Split(New Char() {":"c})
[/highlight]

which would work the same. It looks like you attempted to create an array for the parameter, but fucked it up terribly

[highlight=vb.net]
ip.Split(New Char, (":"{c}))

'..should be:

ip.Split(New Char() {":"c})
[/highlight]

But remember that Split is a function, not a method, meaning it returns the strings so if you want the split values you must assign the result to a variable (as I did in the previous post)
k, ty for your help.

/solved
Marked solve, don't think there should be any reason for anyone to post further.
Found a reason....
returns with ip being 1 and port being 2...
console : "1;2"
Quote Originally Posted by *****179 View Post
Found a reason....
returns with ip being 1 and port being 2...
console : "1;2"
So change the 'split' character to a semicolon rather than a colon?

ip.Split(New Char() {";"c})
No, thats just a exit string i used...
Quote Originally Posted by *****179 View Post
No, thats just a exit string i used...
A what now? What is the problem?
The problem is, its not spliting. Just taking the first and last value from the string.
Posts 1–13 of 13 · Page 1 of 1
This thread is closed for replies.
Talk with us