kh5395
asked on
finging the length of a string
Is there a function in VB that returns the length of a given string in characters.
Thanks
Thanks
Use function len.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Len() returns a Long, not Integer.
This will cause a runtime error:
Dim x As Integer
Dim strText As String
strText = Space(&H10000)
x = Len(strText)
Debug.Print x
This will cause a runtime error:
Dim x As Integer
Dim strText As String
strText = Space(&H10000)
x = Len(strText)
Debug.Print x
kh,
I'm not sure how to say this without sounding like a bit of an a****le, so please don't take this the wrong way. The "len" function is a fairly basic VB function, which leads me to believe you are fairly new at the language. In that case I would strongly advise you to buy a programmer's guide. If you go to a decent book shop which has a "computing" section you should be able to find a good book that will help you get over the learning curve. VB is a very nice language, and there's plenty of good work in it, so it would be money well spent.
I'm not sure how to say this without sounding like a bit of an a****le, so please don't take this the wrong way. The "len" function is a fairly basic VB function, which leads me to believe you are fairly new at the language. In that case I would strongly advise you to buy a programmer's guide. If you go to a decent book shop which has a "computing" section you should be able to find a good book that will help you get over the learning curve. VB is a very nice language, and there's plenty of good work in it, so it would be money well spent.
' you can also return width of a given string in characters
Private Sub Form_Click()
Me.ScaleMode = vbCharacters
MsgBox TextWidth("HHHHH") ' returns 5
MsgBox TextWidth("wwwww") ' returns 5
MsgBox TextWidth("Hello there") ' returns 6.375
End Sub
Private Sub Form_Click()
Me.ScaleMode = vbCharacters
MsgBox TextWidth("HHHHH") ' returns 5
MsgBox TextWidth("wwwww") ' returns 5
MsgBox TextWidth("Hello there") ' returns 6.375
End Sub
Good point that Len returns a Long data type, not an integer - though this only becomes an issue when the length of the string exceeds 32,768 characters or so. I don't often hit that limit when using Len()
25 years ago, programmers never thought their code would still be running in the year 2000.
:-o
:-o