Solved

String Lenght Compare

Posted on 2013-01-04
6
258 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now