Split String New Line After 3 Space in VB.net

Viewed 392

i have problem to split string into newline in vb.net. right now i can make it to split by a single space.i want split new line after 3 space.

Dim s As String = "SOMETHING BIGGER THAN YOUR DREAM" 
Dim words As String() = s.Split(New Char() {" "c})

For Each word As String In words
      Console.WriteLine(word)                                         
Next

output :
SOMETHING 
BIGGER 
THAN 
YOUR 
DREAM

Desire output :
SOMETHING BIGGER THAN 
YOUR DREAM
4 Answers

Another alternative added to existing efficient answers might to be:

Dim separator As Char = CChar(" ")
Dim sArr As String() = "SOMETHING BIGGER THAN YOUR DREAM".Split(separator)
Dim indexOfSplit As Integer = 3

Dim sFinal As String = Join(sArr.Take(indexOfSplit).ToArray, separator) & vbNewLine &
                       Join(sArr.Skip(indexOfSplit).ToArray, separator)

Console.WriteLine(sFinal)

You can split your input string, then loop the array of parts generated and add them to a StringBuilder object.
When you have read a number of parts that is multiple of a defined value, (wordsPerLine, here), you append vbNewLine to the current part.

When the loop completes, print the content of the StringBuilder to the Console:

Dim input As String = "SOMETHING BIGGER THAN YOUR DREAM, NOT MORE THAN YOUR ACCOUNT BALANCE"

Dim wordsPerLine As Integer = 3
Dim wordsCounter As Integer = 1
Dim sb As StringBuilder = New StringBuilder()

For Each word As String In input.Split()
    sb.Append(word & If(wordsCounter Mod wordsPerLine = 0, vbNewLine, " "))
    wordsCounter += 1
Next
Console.WriteLine(sb.ToString())

Prints:

SOMETHING BIGGER THAN
YOUR DREAM, NOT
MORE THAN YOUR
ACCOUNT BALANCE 

Instead of using split, you might capture 3 words in a capturing group and match the trailing whitespace chars.

In the replacement use the group followed by a newline.

Pattern

(\S+(?:\s+\S+){2})\s*

That will match:

  • ( Capture group 1
    • \S+ Match 1+ non whitespace chars
    • (?:\s+\S+){2} Repeat 2 times matching 1+ whitespace chars and 1+ non whitespace chars
  • ) Close group 1
  • \s* Match trailing whitespace chars

.NET Regex demo | VB.NET demo

Example code

Dim s As String = "SOMETHING BIGGER THAN YOUR DREAM"
Dim output As String = Regex.Replace(s, "(\S+(?:\s+\S+){2})\s*", "$1" + Environment.NewLine)
Console.WriteLine(output)

Output

SOMETHING BIGGER THAN
YOUR DREAM

String.Join has an overload that will help you.

First parameter is the character to use between elements of your array.

Second parameter is the array you wish to join.

Third parameter is the starting position, for the first line in your desired output this would be the element at index 0.

Fourth parameter is the length to use, for the first line we want three array elements.

Private Sub OPCode()
    Dim s As String = "SOMETHING BIGGER THAN YOUR DREAM"
    Dim words As String() = s.Split(New Char() {" "c})
    Dim line1 As String = String.Join(" ", words, 0, 3)
    Console.WriteLine(line1)
    Dim line2 As String = String.Join(" ", words, 3, words.Length - 3)
    Console.WriteLine(line2)
End Sub
Related