How to split in VB.NET

I am using VB.NET code.

I have the line below.

http://localhost:3282/ISS/Training/SearchTrainerData.aspx

      

Now I want to split the above line with "/". I want to store the SearchTrainerData.aspx value in a variable.

In my case

Dim str as String

str = "SearchTrainerData.aspx"

      

What would be the code that will strip the above string and store it in a variable?

+2


source to share


5 answers


The Split function accepts the characters that VB.NET represents by appending a 'c' to the end of the one-character string:



Dim sentence = "http://localhost:3282/ISS/Training/SearchTrainerData.aspx"
Dim words = sentence.Split("/"c)
Dim lastWord = words(words.length - 1)

      

+2


source


Try using the String.Split function .



+5


source


Your "string" is obviously a URL, which means you have to use the System.Uri class .

Dim url As Uri = New Uri("http://localhost:3282/ISS/Training/SearchTrainerData.aspx")
Dim segments As String() = url.Segments
Dim str As String = segments(segments.Length - 1)

      

It will also allow you to get all sorts of other interesting information about your "string" without having to resort to manual (and error-prone) parsing.

+5


source


I assume you are actually looking for System.Uri Class. Which makes all the line splitting you're looking for obsolete.

MSDN Documentation for System.Uri

Uri url = new Uri ("http://...");
String[] parts = url.Segments;

      

+1


source


Use split () . You call it on a string instance passing a char array of delimiters and it returns you an array of strings. Take the last item to get your "SearchTrainerData.aspx".

0


source







All Articles