Abbas1999
Abbas1999

Reputation: 395

VB.Net Extract Part of a String Before Certain Character

In VB.NET; I am trying to extract Part of a String Before Certain Character; this is very easy if you have that character once; like below:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label1.Text = TextBox1.Text.Substring(0, TextBox1.Text.IndexOf("/"))  
End Sub

The problem is that my string is a website address that I want to get the first part as below:

http://www.example.com/subfol1/subfol2/abcd.html

I want to get the homepage addres; i.e.:

http://www.example.com/

I tried the following code also:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label1.Text = TextBox1.Text.Split("/"c)(0) 
    Label2.Text = TextBox1.Text.Split("/"c)(1) 
End Sub

But VB.Net always consider the first "/" and ignore the rest?!

I think the idea is to get the part of the string before the third "/", but I do not know how to do that?

Thanks for help

Upvotes: 0

Views: 4918

Answers (1)

prospector
prospector

Reputation: 3469

Set the starting index past the http://

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label1.Text = TextBox1.Text.Substring(11, TextBox1.Text.IndexOf("/", 7) -15)  
End Sub

Upvotes: 1

Related Questions