• 0

Split a String in VB.NET


Question

Hey guys....a VB.NET question....

I want to split a string in such a way that only part before the delimiter is returned. I'm new to VB.NET so please help me out....

i.e.

The delimiter is "*"

The original string is "welcome*friends".

The returned string after being split is "welcome".

The easiest solution to doing this is what I am looking for. I checked the web and there are a lot of complicated ways. Just wanna find an easy method.

Any help is appreciated. Thanks in advance to anyone who can help out. :D

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Dim x As String = "Welcome*Friends"

x = x.Split("*"c)(0)

Console.WriteLine(x)

there's one way to do it.

or if you want a function:

Function ReturnFirstWord(ByVal originalString As String) As String

Return originalString.Split("*"c)(0)

End Function

Link to comment
Share on other sites

  • 0

If you only want the first part, why not just use the substring method (actually the Left method which actually uses Substring)

Dim x as String = "Hello*World"

x = x.Left(x, x.IndexOf("*") + 1)

Link to comment
Share on other sites

  • 0
Hey guys....a VB.NET question....

I want to split a string in such a way that only part before the delimiter is returned. I'm new to VB.NET so please help me out....

i.e.

The delimiter is "*"

The original string is "welcome*friends".

The returned string after being split is "welcome".

The easiest solution to doing this is what I am looking for. I checked the web and there are a lot of complicated ways. Just wanna find an easy method.

Any help is appreciated. Thanks in advance to anyone who can help out. :D

Try this: (I didn't write it, but use it quite often)

Public Function StringToArrayOfWords( _

ByVal stringIn As String, _

ByVal delimitChar As String) _

As String()

' Arguments : stringIn - String to convert

' delimitChar - Character used to delimit words in strIn

' Returns : An array of words

Dim z As String

Dim retVal As String()

retVal = stringIn.Split(CType(delimitChar, Char))

Return retVal

End Function

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.