Program to Split String in VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 275
Suppose we have a text as

Please ignore this line
UserName: RNA Team
Country: India



We need to get the UserName and Country value. The below program will do so

Imports System.Data
Imports System.Linq
Imports System.Text.RegularExpressions

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())

Dim inputText = "Please ignore this line" & vbCr & vbLf & " UserName: RNA Team" & vbCr & vbLf & " Country: India"
'pick up those lines where : is present
'remove the unwanted white spaces
inputText.Split(New String() {vbCr & vbLf}, StringSplitOptions.None).Where(Function(s) s.IndexOf(":"C) <> -1).[Select](Function(s) s.Trim()).ToList().ForEach(Function(line)
Dim txt = Regex.Split(line, ":")
Console.WriteLine("{0} : {1}", txt(0), txt(1))

End Function)

System.Console.ReadKey()

End Sub
End Class

End Namespace

/*

Result
--------
UserName : RNA Team
Country : India


*/

Comments or Responses

Login to post response