Solved

String Lenght Compare

Posted on 2013-01-04
6
257 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to TommySzalapski, this is what I was looking for.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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 …

772 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

13 Experts available now in Live!

Get 1:1 Help Now