Split text in two columns in VB.NET

This is a function code snippet to split a text in to two or more columns in VB.NET.

It is converted from my previous posted: Split text into multiple columns in PHP

Parameters:
InputString – String to be split into columns
Columns – Number of columns
SplitString – String to look for to not split midtext etc

Example of how to use:
[code lang=”vb”]
Dim strColumns As String() = SplitIntoColumns(strText, 2, "<h2>")
strText = "<table><tr><td>" & strColumns(0) & "</td><td>" & strColumns(1) & "</td></tr></table>"
[/code]

Code snippet:
[code lang=”vb”]
Function SplitIntoColumns(ByVal InputString As String, ByVal Columns As Integer, ByVal SplitString As String) As String()

‘ Source: http://blog.tjitjing.com

‘ Splits a string into x number of columns and returns result as an array of columns

‘ Parameters:
‘ $InputString String to be split into columns
‘ $Columns Number of columns
‘ $SplitString String to look for to not split midtext etc

‘ Change history:
‘ 2011-02-04/Max Version 1
‘ 2011-05-16/Max Version 1.2

Dim Output() As String
ReDim Output(0 To Columns – 1)

‘ Find middle of text
Dim ColLength As Integer = Len(InputString) / Columns

‘ Split into columns
Dim ColCount As Integer
Dim Pos As Integer, LastPos As Integer = 0
For ColCount = 1 To Columns

‘ Find $SplitString, position to cut
Pos = -1
If (LastPos + ColLength) < InputString.Length Then
Pos = InputString.IndexOf(SplitString, LastPos + ColLength)
End If
If Pos = -1 Then Pos = InputString.Length

‘ Cut out column
Output(ColCount – 1) = Mid(InputString, LastPos + 1, Pos – LastPos)

LastPos = Pos

Next ColCount

Return Output

End Function

[/code]

[Edit: Updated function]

Split text into multiple columns in PHP

I made a Swedish shopping directory using affiliate links for a site and wanted to split it into two columns.

I wrote the following php function that can be used to split a string in two or more pieces. One parameter is at what character or string you want to split. For example if you want to split at a space to not break up words, or split at a period to not split up sentences, at certain html tags such as <h1> to keep headlines and text in same column etc.

The function returns a string array with columns.

Example how to use:
[code lang=”php”]
$TextString = "some text to split")
$Columns = SplitIntoColumns ($TextString, 2, "<h1>");
echo ($Columns[0]);
echo ($Columns[1]);
[/code]

Function
[code lang=”php”]
function SplitIntoColumns ($InputString, $Columns, $SplitString) {

// Source: http://blog.tjitjing.com
//
// Splits a string into x number of columns and returns result as an array of columns
//
// Parameters:
// $InputString String to be split into columns
// $Columns Number of columns
// $SplitString String to look for to not split midtext etc
//
// Change history:
// 2011-01-25/Max Version 1
//

// Find middle of text
$ColLength = strlen($InputString)/$Columns;

// Split into columns
for($ColCount = 1; $ColCount <= $Columns; $ColCount++) {

// Find $SplitString, position to cut
$Pos = strpos($InputString , $SplitString , $LastPos + $ColLength);
if ($Pos === false) {
$Pos = strlen($InputString);
}

// Cut out column
$Output[$ColCount – 1] = substr($InputString, $LastPos, $Pos – $LastPos);

$LastPos = $Pos;

}

return $Output;

}
[/code]

[Edit: VB.NET version: Split text in two columns in VB.NET]