Results 1 to 5 of 5

Thread: finding the longest length of several strings

  1. #1
    Guest

    i know len() will do it...but how do you..

    i have five strings, eg:
    and i assign the lengths to a
    string array, eg:

    tmp(0) = len(string1)
    tmp(1) = len(string2)
    ...
    ...
    tmp(4) = len(string5)

    is there an easier (hah!) way
    to find the longest string other
    than comparing it to each other
    many times?

    thanks for all help in advance,

    **update**
    thanks to all...

    Fox,
    i used your code with a slight
    addition to recall the index of
    the longest string

    Dim A as Long
    Dim Temp as Long
    dim i as integer 'for index

    For A = 0 to 4
    If Len( Text(a) ) > Temp Then
    Temp = Len( Text(a) )
    i = A 'to recall index of longest string
    Endif
    Next

    [Edited by larryn on 05-11-2000 at 10:31 AM]

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    that is probably the easiest way

    if you wanted to, u could come up with more complex ways, but the way you have is probably the easiest

  3. #3
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    You should store the strings into an array, say Text(4):

    Code:
    Dim A as Long
    Dim Temp as Long
    
    For A = 0 to 4
       If Len( Text(a) ) > Temp Then
          Temp = Len( Text(a) )
       Endif
    Next
    
    'Temp is now the longest length
    hope this helps

  4. #4
    Guest
    declare yourself a variable called MaxLength, and another to remember which element is the longest called ENumb...

    Code:
    Maxlength = 0
    ENumb = -1
    for i = 0 to 4
      if val(tmp(i)) > Maxlength then
        maxlength = val(tmp(i))
        ENumb = i
      end if
    next i
    this code doesnt directly compare the elements to eachother, but it updates the ENumb variable everytime it finds an element longer than the previously longest one!

    mail me if you want a better explanation.

    [Edited by wossname on 05-06-2000 at 05:20 PM]

  5. #5
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    You can also write a little function to assign a string to an item of the array, and check at that point the lenght, and the index number, and store that in a variable...

    eg.
    Code:
    Private m_MaxLength As Long
    Private m_MaxLengthIndex As Long
    
    Private Sub AddItem(Index As Long, psValue As String)
        If Len(psValue) > m_MaxLength Then
            m_MaxLengthIndex = Index
            m_MaxLength = Len(psValue)
        End If
        If Index > Ubound(MyArray) Then
            Redim Preserve MyArray(Index)
        End If
        MyArray(Index) = psValue
    End Sub
    m_MaxLength now always contains the length of the longest item, and m_MaxLengthIndex the index of that item in the array.

    [Edited by Crazy D on 05-06-2000 at 08:05 PM]
    Hope this helps

    Crazy D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width