Solved

finging the length of a string

Posted on 2000-03-05
7
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

728 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