HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Splitting company name into 3 strings

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
companyintosplittingnamestrings

Problem

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 > 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 = 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

Solution

Just for the fun of it, here's a one-liner using LINQ.

Dim names = (From i In {0, 35, 70} Where i < 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  0), names(0), String.Empty)
Dim var2 As String = If((names.Length > 1), names(1), String.Empty)
Dim var3 As String = If((names.Length > 2), names(2), String.Empty)

Code Snippets

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

Context

StackExchange Code Review Q#56002, answer score: 7

Revisions (0)

No revisions yet.