Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string in VB.NET

I am trying to split the following into two strings.

"SERVER1.DOMAIN.COM Running"

For this I use the code.

Dim Str As String = "SERVER1.DOMAIN.COM Running"
Dim strarr() As String
strarr = Str.Split(" ")
For Each s As String In strarr
    MsgBox(s)
Next

This works fine, and I get two message boxes with "SERVER1.DOMAIN.COM" and "Running".

The issue that I am having is that some of my initial strings have more than one space.

"SERVER1.DOMAIN.COM        Off"

There are about eight spaces in-between ".COM" and "Off".

How can I separate this string in the same way?

like image 886
Matt Leyland Avatar asked Jun 12 '13 07:06

Matt Leyland


2 Answers

Try this

Dim array As String() = strtemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
like image 156
Sachin Avatar answered Oct 04 '22 18:10

Sachin


Use this way:

Dim line As String = "SERVER1.DOMAIN.COM Running"
Dim separators() As String = {"Domain:", "Mode:"}
Dim result() As String
result = line.Split(separators, StringSplitOptions.RemoveEmptyEntries)
like image 23
Jayram Avatar answered Oct 04 '22 18:10

Jayram