I wrote an algorithm which should cut a companies name into 3 strings.
Input: 1 String
Output: 3 String.
String 1 2 and 3 shall not be longer then 35 signs. If the Input string is longer then it should be cut to a length of 105.
If you have fun and be interested in algorithms it would be nice if you take a look at it.
Public Sub CompanyCut() //3 output Strings Dim var1 As String = "" Dim var2 As String = "" Dim var3 As String = "" If Module1.insertdict("company").Length > 35 Then Dim s1 As String = Module1.insertdict("company").ToString If s1.Length > 105 Then s1 = Microsoft.VisualBasic.Left(s1, 105) End If //Split String into array at every Whitespace Dim pattern As String = "\s" Dim sa() As String = Regex.Split(s1, pattern) //Variables for loop Dim i As Integer = 0 Dim varyn As Boolean = True Dim varyn1 As Boolean = False Dim varyn2 As Boolean = False //loop which fills var1 var2 and var3 with arrayfields untill size 35 would be reached For i = 0 To sa.Length - 1 If var1.Length < 35 AndAlso varyn = True Then If var1.Length + 1 + sa(i).Length < 35 Then var1 = var1 + " " + sa(i).ToString Else varyn = False varyn1 = True varyn2 = False End If End If If var2.Length < 35 AndAlso varyn1 = True Then If var2.Length + 1 + sa(i).Length < 35 Then var2 = var2 + " " + sa(i).ToString Else varyn1 = False varyn = False varyn2 = True End If End If If var3.Length < 35 AndAlso varyn2 = True Then If var3.Length + 1 + sa(i).Length < 35 Then var3 = var3 + " " + sa(i).ToString Else varyn2 = False End If End If Next //my idea was that if it has the same or bigger length all fields must be in + the whitespaces If var1.Length + var2.Length + var3.Length >= s1.Length Then Module1.insertdict("Firma") = var1 Module1.insertdict("Name2") = var2 Module1.insertdict("Name3") = var3 Else //this occurs when the string is smaller as 105 signs but not all fields of the array could be placed in the variables. Module1.insertdict("Failure") = "Company name need to split by user" End If Else Module1.insertdict("Name2") = "" Module1.insertdict("Name3") = "" End If End Sub
**Edit: I forgot to bring up in the Conditions that if it is possible it should be cut at >the whitespace.
What to do now? shall I open a new Post?**
My main problem is to find a sub algorithmn which handles this case
Else //this occurs when the string is smaller as 105 signs but not all fields of the array could be placed in the variables. Module1.insertdict("Failure") = "Company name need to split by user" End If
An easy way would be to cut it with substring like in the answers. a more elegant way would be to cut the first 35 chars for the first variable and then trying to cut at whitespaces. If it doesnt works also cut the second 35 for var2. and the rest is going to var3 :)