|
-
May 18th, 2022, 12:00 PM
#1
Thread Starter
Member
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:
<Extension()>
Public Function Wrap(ByVal Text As String, ByVal Width As Integer, Optional ByVal FullWidthLine As Boolean = False) As String
Dim InputLines As List(Of String) = Text.Split(New Char() {vbCr, vbLf, vbCrLf, Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).ToList
Dim OutputLines As New List(Of String)
For Each Line As String In InputLines
Dim Words As List(Of String) = Line.Split(CChar(" ")).ToList()
If Line.Length < Width OrElse Words.Count = 1 Then
If FullWidthLine Then
OutputLines.Add(Line.PadRight(Width, " "c))
Else
OutputLines.Add(Line)
End If
Else
Dim CurrentLine As String = Words(0)
For I As Integer = 1 To Words.Count - 1
If (CurrentLine & " " & Words(I)).Length > Width Then
If FullWidthLine Then
OutputLines.Add(CurrentLine.PadRight(Width, " "c))
Else
OutputLines.Add(CurrentLine)
End If
CurrentLine = Words(I)
If I = Words.Count - 1 Then
If FullWidthLine Then
OutputLines.Add(CurrentLine.PadRight(Width, " "c))
Else
OutputLines.Add(CurrentLine)
End If
End If
Else
If I = Words.Count - 1 Then
If FullWidthLine Then
OutputLines.Add((CurrentLine & " " & Words(I)).PadRight(Width, " "c))
Else
OutputLines.Add(CurrentLine & " " & Words(I))
End If
End If
CurrentLine &= " " & Words(I)
End If
Next I
End If
Next
Return String.Join(Environment.NewLine, OutputLines.ToArray())
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:
Dim NewLines As Char() = {vbCrLf, vbCr, vbLf, Environment.NewLine}
Dim TestLines As New List(Of String)
Dim LastIndex As Integer = 0
Dim NextIndex As Integer = Text.IndexOfAny(NewLines, LastIndex)
Do While NextIndex > 0
TestLines.Add(Text.Substring(LastIndex, NextIndex))
LastIndex = NextIndex + 1
NextIndex = Text.IndexOfAny(NewLines, LastIndex)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|