Solved

String Lenght Compare

Posted on 2013-01-04
6
259 Views
Last Modified: 2013-01-15
I have an old vb.net application that reads some data from ini file and sets them to few different variables.  Now, I want to be able to compare the lengh of these values and figure out which one which variable has the longest string lenght.
0
Comment
Question by:vb7guy
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38744582
Just use the Len function and some kind of loop to get the max. Something like:
max = str1
If Len(str2) > Len(max) Then
max = str2
End If

Open in new window


If it's only two values, then just compare Len(str1) > Len(str2)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38744750
Or, for a more .Netish approach, use the .Length() property:

    If strA.Length > strB.Length Then

    End If

See String.Length: http://msdn.microsoft.com/en-us/library/system.string.length.aspx

For the second part, if you need to compare a bunch of these variables, do the variable names have any kind of pattern to them?
0
 

Author Comment

by:vb7guy
ID: 38745011
I know how to compare two variables and figure out which one is longer.  I have about 5 variables.  For example.
sStrVal1
sStrVal2
sStrval3
sStrval4
sStrval5
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 38745040
If they are not in some kind of array, then just do this

longest = sStrVal1
max = longest.Length

If sStrVal2.Length > max Then
   longest = sStrVal2
   max = longest.Length
End If
If sStrVal3.Length > max Then
   longest = sStrVal3
   max = longest.Length
End If
If sStrVal4.Length > max Then
   longest = sStrVal4
   max = longest.Length
End If
If sStrVal5.Length > max Then
   longest = sStrVal5
   max = longest.Length
End If

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38745236
Here's an approach if your variables follow a pattern as you've shown.  Note that the variables are declared as PUBLIC:
Public Class Form1

    Public sStrVal1 As String = "abcd"
    Public sStrVal2 As String = "abcde"
    Public sStrVal3 As String = "ab"
    Public sStrVal4 As String = "abcdef"
    Public sStrVal5 As String = "a"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim longestName As String = ""
        Dim longestValue As String = ""

        Dim varName As String
        Dim varValue As String
        For i As Integer = 1 To 5
            varName = "sStrVal" & i
            varValue = CallByName(Me, varName, CallType.Get)
            If longestName = "" OrElse varValue.Length > longestValue.Length Then
                longestName = varName
                longestValue = varValue
            End If
        Next

        MessageBox.Show(longestName & " = " & longestValue)
    End Sub

End Class

Open in new window


*This uses the legacy VB6 CallByName() function which requires those variables to be Public.  The same thing can be done with the newer .Net REFLECTION techniques, allowing access to variables that are Private, but the code isn't as succinct.  Let me know if you want to see an example of that.
0
 

Author Closing Comment

by:vb7guy
ID: 38778027
Thanks to TommySzalapski, this is what I was looking for.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question