0

I need to split some text, but i'm having trouble with some extra spaces. The following image shows the usual output text.

example

I want to add the split values in a checkedlistbox and i have the following code:

Dim SEPARATED = TextBox1.Text.Split(vbCr, vbLf, vbTab, ">") '" "C

    CheckedListBox1.Items.Clear()
    For Each item In separated
        If item <> "" Then CheckedListBox1.Items.Add(item)
    Next
    CheckedListBox1.Items.Remove("#")
    CheckedListBox1.Items.Remove("PI")

    Dim MODIFIED As String() = ANSWER.ToString.Split(" ")

buuuuuuut the extra spaces are giving me headaches. I don't know how to split the text in groups of twelve chars ( they're always 12, the spaces complete them) .

In conclusion, using the example, i want to only show "DEFAULT","EX W SPACE","POTATO" and "HELP PLS".

Thank you !

noidea
  • 184
  • 5
  • 22

1 Answers1

1

I used the GetOPString() function to return a string with your items in it (DEFAULT","EX W SPACE","POTATO" and "HELP PLS") with a padding of 12 characters. That function you do not need because your source string is already built like that.

I used the following logic

  1. Split source string into lines
  2. If there is a word with no space in front (EXAMPLE), It is some sort of title so I ignore it (you didn't want it in the final result)
  3. Trim word to remove all spaces and starting ">" character if found

See the example below.

You can tweak it according to your need.

Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    Dim Rawstring As String = GetOPString("DEFAULT","EX W SPACE","POTATO",">HELP PLS")
    Dim SEPARATED As List(Of String) = GetListItems(Rawstring)

    For Each Item As String In SEPARATED
        CheckedListBox1.Items.Add(Item)
    Next
End Sub



  ''' <summary>
''' Get a list of string by dividing Rawstring into 12 chars sequence. 
''' Any space is trimmed and leading > character (if found) is removed.
''' </summary>
''' <param name="RawString"></param>
''' <returns></returns>
Private Function GetListItems(RawString As String) As IEnumerable(Of String)
    Dim Output As New List(Of String)

    For Each item In RawString.Split(vbCr)
        If String.IsNullOrWhiteSpace(item) Then Continue For

        Dim AssumeTitle As Boolean = Not String.IsNullOrWhiteSpace(item(0))
        If AssumeTitle Then Continue For 'EXAMPLE is the title and we do not want it in the checkbox

        item = item.Trim.TrimStart(">"c)
        Output.Add(item)

    Next



    Return Output
End Function

''' <summary>
''' I used this function only to return the string you have in your post, 
''' which is, words to be separated in 12 characters sequences.
''' </summary>
''' <returns></returns>
Private Function GetOPString(ByVal ParamArray Words As String()) As String
    Dim Output As New Text.StringBuilder
    Output.AppendLine("EXAMPLE")
    Output.AppendLine()

    For Each Item As String In Words
        Output.Append(vbTab)
        If Not Item.StartsWith(">") Then
            Output.Append(" ")
        End If
        Output.AppendLine(Item.PadRight(12))
    Next

    Return Output.ToString
End Function
Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39
  • Sorry for the late comment. What about the beggining of sentence? Everything in the image is the output .. "Example" and the linebreakers as well. – noidea May 30 '16 at 09:07
  • See my edited answer. This will work with the exact string you provided. – Sage Pourpre May 30 '16 at 14:31
  • Your mention of the 12 characters was finally not deterministic. I used the line breaks to get a list of result and ignored "titles" or item that were at the start of the line (no spaces). – Sage Pourpre May 30 '16 at 14:32
  • Pretty good! It was hard to understand how, but i got there already ! Thank you – noidea May 30 '16 at 15:00