Splitting company name into 3 strings I wrote an algorithm which should cut a companies name into 3 strings.


  Input: 1 String
  
  Output: 3 String.
  
  Conditions:
  
  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 &gt; 35 Then



        Dim s1 As String = Module1.insertdict("company").ToString

        If s1.Length &gt; 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 &lt; 35 AndAlso varyn = True Then
                If var1.Length + 1 + sa(i).Length &lt; 35 Then
                    var1 = var1 + " " + sa(i).ToString
                Else
                    varyn = False
                    varyn1 = True
                    varyn2 = False

                End If

            End If

            If var2.Length &lt; 35 AndAlso varyn1 = True Then
                If var2.Length + 1 + sa(i).Length &lt; 35 Then
                    var2 = var2 + " " + sa(i).ToString
                Else
                    varyn1 = False
                    varyn = False
                    varyn2 = True

                End If

            End If

            If var3.Length &lt; 35 AndAlso varyn2 = True Then
                If var3.Length + 1 + sa(i).Length &lt; 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 &gt;= 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 :)
Just for the fun of it, here's a one-liner using LINQ.

Dim names = (From i In {0, 35, 70} Where i &lt; input.Length Select input.Substring(i, Math.Min(35, (input.Length - i))))


Example

Dim names As String() = (From i In {0, 35, 70} Where i &lt; input.Length Select input.Substring(i, Math.Min(35, (input.Length - i)))).ToArray()
Dim var1 As String = If((names.Length &gt; 0), names(0), String.Empty)
Dim var2 As String = If((names.Length &gt; 1), names(1), String.Empty)
Dim var3 As String = If((names.Length &gt; 2), names(2), String.Empty)