Solved

finging the length of a string

Posted on 2000-03-05
7
246 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

808 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