Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String Lenght Compare

Posted on 2013-01-04
6
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 1500 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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project

721 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