-1

I have a problem while making my skype resolver, when it resolves an ip the text comes out like this ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,SERVER 1: 124.169.179.116 - Skype4Resolver.com" (btw the "," are spaces... stackoverflow doesn't show them.) when this happeneds it makes the text go unseen and out of the textbox. can anyone help? i want to filter out the spaces and anything that isnt part of the ip. here is my code:

Imports System.Net

Public Class Form2


Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    WebRequest.Create("http://api.skype4resolver.com/api.php?key=free&username=" + TextBox1.Text     + "&server=1")
    TextBox2.Text = New    System.Net.WebClient().DownloadString("http://api.skype4resolver.com/api.php?key=free&username=" + TextBox1.Text + "&server=1")
End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

End Sub
End Class

2 Answers2

2

Use a trim function on the returned string!

http://msdn.microsoft.com/en-us/library/system.string.trimstart(v=vs.110).aspx

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Peter Healy
  • 223
  • 1
  • 9
0

Depending on what you want, try a regular expression (regexp based on RegEx for an IP Address) or split on the colon (as per http://msdn.microsoft.com/en-us/library/tabh47cf%28v=vs.110%29.aspx)

Imports System.Text.RegularExpressions
Module Module1

 Sub Main()
    Dim Input As String = "                                       SERVER 1: 124.169.179.116 - Skype4Resolver.com"

    Dim ResultSplit As String = Input.Split(":")(1)
    Console.WriteLine(String.Format("Split token [{0}]", ResultSplit)) ' Split token [124.169.179.116 - Skype4Resolver.com]

    Dim IpRegex As Regex = New Regex("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")
    If (IpRegex.IsMatch(Input)) Then
        Dim ResultRegex As MatchCollection = IpRegex.Matches(Input)
        Console.WriteLine(String.Format("Regexp match [{0}]", ResultRegex(0))) ' Regexp match [124.169.179.116]
    End If

    Console.WriteLine()

 End Sub

End Module

which in your specific use case becomes

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Input As String = TextBox1.Text
    Dim IpRegex As Regex = New Regex("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")
    If (IpRegex.IsMatch(Input)) Then
        Dim ResultRegex As MatchCollection = IpRegex.Matches(Input)

        WebRequest.Create("http://api.skype4resolver.com/api.php?key=free&username=" + ResultRegex(0) + "&server=1")

        TextBox2.Text = ResultRegex(0)
    End If
End Sub

Regular expressions are very powerful for text processing. Make a point of learning them eventually. You want to discard the part that you don't want, but instead I go find the part that I do want. So it won't matter if the spaces are commas or if the server name is in a foreign language: this regular expression will find the first thing that looks like an IP address and match it.

Community
  • 1
  • 1
woodvi
  • 1,898
  • 21
  • 27
  • but the ip changes every time you use it depending on who you resolve? – pstefa Stefa Dec 22 '14 at 22:50
  • is there any way just to say Deleate ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,SERVER 1: & - Skype4Resolver.com ? so then the ip is the only thing there? btw the ,,, are spaces – pstefa Stefa Dec 22 '14 at 22:58
  • Thanks heaps! looks like it would work but i imported System.Text.RegularExpressions and when i try to run it throws 2 errors.... This one 'Error 2 Value of type 'System.Text.RegularExpressions.Match' cannot be converted to 'String'. C:\Users\owner\Documents\Visual Studio 2012\Projects\WindowsApplication2\WindowsApplication2\Form2.vb 20 29 WindowsApplication2 ' and 'Error 1 Operator '+' is not defined for types 'String' and 'System.Text.RegularExpressions.Match'. C:\Users\owner\Documents\Visual Studio 2012\Projects\WindowsApplication2\WindowsApplication2\Form2.vb 18 31 WindowsApplication2 ' – pstefa Stefa Dec 24 '14 at 03:06
  • The first version compiled and ran for me. The second version was adapted to what I saw in your procedure, but I didn't test it. Try replacing any reference to `ResultRegex(0)` to `ResultRegex(0).ToString()` or `ResultRegex(0).Value` as per http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match%28v=vs.110%29.aspx – woodvi Dec 26 '14 at 15:21