Solved

finging the length of a string

Posted on 2000-03-05
7
231 Views
Last Modified: 2010-05-02
Is there a function in VB that returns the length of a given string in characters.

Thanks
0
Comment
Question by:kh5395
7 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2585192
Use function len.
0
 

Accepted Solution

by:
aaronspitzer earned 50 total points
ID: 2585502
Yes there is: Len()

Usage is straightforward-

Dim x as Integer
Dim strText as String

strText = "Hello there"

x = Len(strText)

debug.Print x

This little routine will return 11 in the immediate window.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2585684
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
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Expert Comment

by:caraf_g
ID: 2585714
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.
0
 
LVL 15

Expert Comment

by:ameba
ID: 2585872
' 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
0
 

Expert Comment

by:aaronspitzer
ID: 2589218
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()
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2589385
25 years ago, programmers never thought their code would still be running in the year 2000.

:-o
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

17 Experts available now in Live!

Get 1:1 Help Now