|
-
May 5th, 2000, 02:27 AM
#1
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]
-
May 5th, 2000, 09:50 AM
#2
Conquistador
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
-
May 5th, 2000, 10:13 PM
#3
PowerPoster
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
-
May 5th, 2000, 10:18 PM
#4
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]
-
May 6th, 2000, 07:04 AM
#5
Hyperactive Member
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]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|