1

Given a String...

M0 Apple Sauce M1 Peanut Butter M2 Porcupine M3 Quesadilla M4 Margarita

How could I split that String into..

M0 Apple Sauce
M1 Peanut Butter
M2 Porcupine
M3 Quesadilla
M4 Margarita 

Is there a way to put M0, M1,..., M4 in an array, and then use that array to split the string?

6
  • 3
    Better, use a regex - Regex.Split(s, "(?!^)(?=\bM\d+\b)"). Commented Apr 11, 2017 at 17:58
  • @WiktorStribiżew I was just reading about that, could you give an example using my above example? Commented Apr 11, 2017 at 18:02
  • What kind of output are you looking for? (e.g., DataTable with 2 columns, 1 column, or an array of strings?) Commented Apr 11, 2017 at 18:03
  • See this demo, Split List. Commented Apr 11, 2017 at 18:03
  • Just tested Wiktor's regex string and it works brilliantly. Commented Apr 11, 2017 at 18:07

3 Answers 3

3

Full credit to Wiktor Stribizew:

Imports System.Text.RegularExpressions
Public Class frmMain
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim stString As String = "M0 Apple Sauce M1 Peanut Butter M2 Porcupine M3 Quesadilla M4 Margarita"
        Dim arrString() As String
        arrString = Regex.Split(stString, "(?!^)(?=\bM\d+\b)")
        For Each st As String In arrString
            MessageBox.Show(st)
        Next
    End Sub
End Class

Regex explanation:

The fact that the regex string is being used in a split method means that your regex pattern's goal is to find positions in the string upon which to split.
The first segment of the regex string !?^ simply asserts that there is to be no 'match' at position 0. We do have a match at position 0, but we don't want a 0-length string in the output, so we use this stipulation. I'll breakdown the second subexpression ?=\bM\d+\b:

  • ?= is used to say "we're looking ahead of the cursor for matches of the following pattern" - without consuming the text
  • \b adds that the match must occur where an alphanumeric character is beside a non-alphanumeric character (in this case, always a space and an M); this is any 'word boundary'
  • M is indeed the literal character match of M
  • \d matches any decimal digit
  • + means that there may be any number of decimal digits in a row at this point (one or more decimal digits)
  • The last \b details that the digit(s) end with another word boundary
Sign up to request clarification or add additional context in comments.

7 Comments

Awesome, I'm confused as to why "(?!^)(?=\bM\d+\b)" does the job?
@Bob, Yeah, you'd have to brush up on regular expressions syntax to fully understand it.
But simply put... that expression looks for the letter M plus an integer? The code i'm going to be using this in will have M's all the way up to M60. Will it work for that?
You may keep it, just explain what the pattern means. Without explanation, a great number of regex-related answers are not helpful.
Yeah, almost. (?=...) is a positive lookahead that only checks for the pattern match, but does not consume text. \b matches a word boundary (not just alphanumeric/non-alpha transitions). + match 1 or more instances, not any.
|
1

Use Regex.Split method to split parts with regular expression.

    Dim stringToSplit As String = "M0 Apple Sauce M1 Peanut Butter M2 Porcupine M3 Quesadilla M4 Margarita"
    Dim stringParts = Regex.Split(stringToSplit, "(?!^)(?=\bM\d+\b)")
    For i = 0 To stringParts.Length - 1
        Console.WriteLine(stringParts(i))
    Next

This code will produce following output:

M0 Apple Sauce

M1 Peanut Butter

M2 Porcupine

M3 Quesadilla

M4 Margarita

Comments

0

Regex is a great answer for this kind of wonky stuff. But there are other ways if you don't like Regex. The Step keyword can do this.

Dim sText As String = "M0 Apple Sauce M1 Peanut Butter M2 Porcupine M3 Quesadilla M4 Margarita"
    Dim split() As String = sText.Trim.Split(" ")
    Dim sResult As String = String.Empty
    For i = 0 To UBound(split) Step 2
        sResult &= split(i) & " " & split(i + 1) & vbCrLf
    Next
    MsgBox(sResult)

Just remember to ensure the data is always in pairs, and each element is separated by a space.

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.