Solved

finging the length of a string

Posted on 2000-03-05
7
241 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

809 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