Results 1 to 5 of 5

Thread: String Split Remove SOME Empty Entries

Threaded View

  1. #1

    Thread Starter
    Member G_Hosa_Phat's Avatar
    Join Date
    May 2008
    Location
    Oklahoma City, OK
    Posts
    44

    String Split Remove SOME Empty Entries

    I'm trying to create an extension method for wrapping text to a specific number of characters for display in a console application. For the most part, it works very well, but I've run into a slight issue with how it handles a string that contains line breaks (Environment.NewLine, vbCr, vbLf, vbCrLf). Here's the method I'm currently working with:
    vbnet Code:
    1. <Extension()>
    2.     Public Function Wrap(ByVal Text As String, ByVal Width As Integer, Optional ByVal FullWidthLine As Boolean = False) As String
    3.         Dim InputLines As List(Of String) = Text.Split(New Char() {vbCr, vbLf, vbCrLf, Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).ToList
    4.         Dim OutputLines As New List(Of String)
    5.  
    6.         For Each Line As String In InputLines
    7.             Dim Words As List(Of String) = Line.Split(CChar(" ")).ToList()
    8.  
    9.             If Line.Length < Width OrElse Words.Count = 1 Then
    10.                 If FullWidthLine Then
    11.                     OutputLines.Add(Line.PadRight(Width, " "c))
    12.                 Else
    13.                     OutputLines.Add(Line)
    14.                 End If
    15.             Else
    16.                 Dim CurrentLine As String = Words(0)
    17.  
    18.                 For I As Integer = 1 To Words.Count - 1
    19.                     If (CurrentLine & " " & Words(I)).Length > Width Then
    20.                         If FullWidthLine Then
    21.                             OutputLines.Add(CurrentLine.PadRight(Width, " "c))
    22.                         Else
    23.                             OutputLines.Add(CurrentLine)
    24.                         End If
    25.  
    26.                         CurrentLine = Words(I)
    27.  
    28.                         If I = Words.Count - 1 Then
    29.                             If FullWidthLine Then
    30.                                 OutputLines.Add(CurrentLine.PadRight(Width, " "c))
    31.                             Else
    32.                                 OutputLines.Add(CurrentLine)
    33.                             End If
    34.                         End If
    35.                     Else
    36.                         If I = Words.Count - 1 Then
    37.                             If FullWidthLine Then
    38.                                 OutputLines.Add((CurrentLine & " " & Words(I)).PadRight(Width, " "c))
    39.                             Else
    40.                                 OutputLines.Add(CurrentLine & " " & Words(I))
    41.                             End If
    42.                         End If
    43.  
    44.                         CurrentLine &= " " & Words(I)
    45.                     End If
    46.                 Next I
    47.             End If
    48.         Next
    49.  
    50.         Return String.Join(Environment.NewLine, OutputLines.ToArray())
    51.     End Function

    For a string like $"Invalid selection. Option {UserInput} is not available." & vbCrLf & "Please enter the number of the desired menu item from the list below." This works exactly as expected. The output is "wrapped" to two lines and displayed pretty much exactly as it was entered:

    Code:
    Invalid selection. Option 9 is not available.
    Please enter the number of the desired menu item from the list below.
    However, if I want to add an additional line of space between the two lines like this: $"Invalid selection. Option {UserInput} is not available." & vbCrLf & vbCrLf & "Please enter the number of the desired menu item from the list below." the StringSplitOptions.RemoveEmptyEntries wipes out my "extra" line and I end up with exactly the same thing as before:

    Code:
    Invalid selection. Option 9 is not available.
    Please enter the number of the desired menu item from the list below.
    Now, this is to be expected because the extra line break is "technically" an empty entry. But, in this case, I actually want that extra bit of space between the two lines.

    Code:
    Invalid selection. Option 9 is not available.
    
    Please enter the number of the desired menu item from the list below.
    If I remove the StringSplitOptions.RemoveEmptyEntries parameter from my Split method call (or change it to StringSplitOptions.None), then I end up with additional empty lines in my OutputLines List(Of String) object that "don't belong" there.

    So, now I'm just trying to figure out how to build my List(Of String) object, split on any of the new line character(s), preserving blank lines, but not "creating" empty entries.

    In my testing, I've tried to do this:
    vbnet Code:
    1. Dim NewLines As Char() = {vbCrLf, vbCr, vbLf, Environment.NewLine}
    2.         Dim TestLines As New List(Of String)
    3.         Dim LastIndex As Integer = 0
    4.         Dim NextIndex As Integer = Text.IndexOfAny(NewLines, LastIndex)
    5.  
    6.         Do While NextIndex > 0
    7.             TestLines.Add(Text.Substring(LastIndex, NextIndex))
    8.             LastIndex = NextIndex + 1
    9.             NextIndex = Text.IndexOfAny(NewLines, LastIndex)
    10.         Loop
    But, that ends up creating "duplicate" lines in my list because it's hitting each of the individual new line characters - vbCr (Chr(13)) and vbLf (Chr(10)) - and creating a new line for that. I tried just using Environment.NewLine or vbCrLf, but those don't seem to catch things correctly either and I end up with just two lines - everything up to the first line break, then one for everything after the new line characters at the beginning. It's still not what I'm after and I'm just not sure how to get there. Any assistance would be greatly appreciated.
    Last edited by G_Hosa_Phat; May 18th, 2022 at 12:13 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width