Reputation: 398
Hi I have a string like this
MRP^1^1~MRP|MRP+1^1^1~MRP+1|MRP+2^1^1~MRP+2|MRP+3^1^1~MRP+3|MRP+4^1^1~MRP+4
I want to split it and get the into an array like this in a single line operation.
MRP
MRP+1
MRP+2
MRP+3
MRP+4
Can any one help me
Upvotes: 1
Views: 301
Reputation: 149295
Here is another way to do it using Regex :)
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
'~~> Input string
Dim strSample As String = "MRP^1^1~MRP|MRP+1^1^1~MRP+1|MRP+2^1^1~MRP+2|MRP+3^1^1~MRP+3|MRP+4^1^1~MRP+4"
Dim pattern As String = "(MRP\+?[0-9]?)\^1\^1\~"
Dim matches As MatchCollection = Regex.Matches(strSample, pattern)
For Each match As Match In matches
MessageBox.Show (Match.Groups(1).Value)
Next
End Sub
End Class
EDIT
There can be one more way (A bit long winded though...)
You can use String.Replace to replace "^1^1~" by "|" so you have just 1 delimiter and then use Split to extract unique values. But then Like I mentioned above, that is kind of long winded. :)
HTH
Sid
Upvotes: 2
Reputation: 3829
While this line of code will give you the result you seek, it may not be the best approach since I don't know how the data may variate nor what defines it, perhaps a little more insight on what you're trying to accomplish and where does that string come from may help us give you a better answer.
Dim resultArray = myCurrentString.Split(New Char() {"~", "|"}).Where(Function(str) Not str.Contains("^")).ToArray
Upvotes: 1