I have a string which is being entered by user, length depends on the length of entered string. In another field user needs to type a number of how many equal parts he wants to get out of that string.
For example:
Dim unos As String = "Jedna duga linija teksta koju zelim prelomiti na dijelove"
Now I want to split this into string array. Each part of this string depends on entered number of parts, example
EDIT:
I have this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim unos As String = TextBox1.Text
Dim brLinija As Integer = TextBox2.Text
Dim unosNiz As New List(Of String)
For i As Integer = 0 To Convert.ToInt32(unos.Length / brLinija) - 1
unosNiz.Add(unos.Substring(i, unos.Length / brLinija))
Next
For Each s As String In unosNiz
TextBox3.Text = TextBox3.Text & s & vbCrLf
Next
End Sub
brLinija is integer entered by user (number of lines of string array)
User entered number of lines: 4
Current output:
Jedna duga lin
edna duga lini
dna duga linij
na duga linija
a duga linija
duga linija t
duga linija te
uga linija tek
ga linija teks
a linija tekst
linija teksta
linija teksta
inija teksta k
nija teksta ko
Jedna duga lin
edna duga lini
dna duga linij
na duga linija
a duga linija
duga linija t
duga linija te
uga linija tek
ga linija teks
a linija tekst
linija teksta
linija teksta
inija teksta k
nija teksta ko
Output I want:
jedna duga lini
ja teksta koju
zelim prelomiti
na dijelove
I'm just confused how I'm supposed to split this string properly into a substrings.
Thanks
So, you probably simply want an equal number of chars per line. The last line can instead contain just the remainder.
You can iterate the number of lines specified (provided that the number can be parsed to an integer, so use Integer.TryParse() to validate the input), then divide the string length in chars by the number of lines and take that number of chars per iteration.
Something like this:
Imports System.Linq
' You should also check whether the TextBox.Text contains any chars
Dim unos As String = TextBox1.Text
Dim brLinija As Integer = 0
If (Not Integer.TryParse(TextBox2.Text, brLinija)) Then
MessageBox.Show("Invalid input")
Return
End If
Dim charPerPart = CInt(Math.Ceiling(unos.Length / brLinija))
Dim unosNiz As New List(Of String)
For i As Integer = 0 To brLinija - 1
unosNiz.Add(String.Concat(unos.Skip(charPerPart * i).Take(charPerPart)))
Next
It generates this output:
Jedna duga lini
ja teksta koju
zelim prelomiti
na dijelove