Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Focus On Visual Basic
A special Focus Section of Visual Basic Explorer

Part 1

There are a lot of skills that go into making a 'good' programmer and a one of the bread and butter skills is the ability to manipulate strings. If you want to combine, search, replace, slice or dice some text this tutorial by Brian Duckworth will introduce you to some of the key issues involving strings.

<< Main] [Next >>


Visual Basic is a "large" language.  Its language constructs, integration into the Windows environment and localization features make a topic as deceptively simple as strings more complex than it might first appear.

The material presented here serves as an introduction to the majority of string related issues.  There are, of course, more advanced issues which are outside the scope of this article.  Additional resources can be found by following the links on the main page of this Focus On Visual Basic topic.

What's a string?

In Visual Basic, a string is text composed of a sequence of zero or more characters.  A string might contain alphabetic characters, numbers and/or symbols.

Internally, each character of a string is represented by a numeric value called a character code (sometimes refered to as an ASCII code).  Character codes range from 0 to 255.  The use of character codes is important when using or identifying non-printable characters (like the Return key) or characters which are not part of the standard keyboard (like "Ø").

The following will generate a list of characters and their corresponding character codes:

Dim i As Integer

For i = 0 To 255
  Debug.Print i; Chr$(i)
Next

For a table showing the character codes from 0 - 127 click here or for the character codes from 128 - 255 click here.

Strings may be held in variables declared as type String or Variant.  They may also be represented as constant values.  A constant string is a sequence of characters surrounded by quotation marks (").

'A variable which will hold a string value.
Dim sName As String

'A string constant being assigned to the string variable.
sName = "Basic"

To place a quotation mark (") inside a string constant, you must either place two quotes together:
'Placing quotes inside a string constant.
sName = "A ""quoted"" constant."
'sName now holds: A "quoted" constant
or build a string using the character code for a quote (34):
'Placing quotes inside a string using the Chr$ function:
sName = "A " & Chr$(34) & "quoted" & Chr$(34) & " constant."
'sName now holds: A "quoted" constant
Later, we'll talk more about building strings, the "&" operator and the Chr$ function.

In Visual Basic, you can declare two types of string variables: variable-length and fixed-length.  Variable-length string variables can contain string values of any length from 0 to about 2 billion (2^31) characters.  Fixed-length strings can contain string values of any length from 1 to over 65 thousand (2^16) characters.

'A variable-length string declaration
Dim sVariable As String

'A fixed-length string declaration
Dim sFixed As String * 10

Fixed-length strings have the characteristic of always containing the number of characters with which they're declared.  In the example above, strFixed will always contain strings of exactly 10 characters, whereas strVariable may contain strings of different lengths.  If a string of fewer than 10 characters is assigned to strFixed, spaces will be added to the end of the string until strFixed contains exactly 10 characters.  However, if a string of more than 10 characters is assigned, only the first 10 will be stored; any remaining characters will be lost.
sFixed = "Small"          'strFixed contains "Small     "
sFixed = "It's too long"  'strFixed contains "It's too l"

<< Main] [Next >>





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.