Visual Basic/Strings

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Visual Basic has a traditional set of built in string operations. Unlike many languages Visual Basic strings are always Unicode so they can hold any character. They are also always dynamically allocated and are of almost unlimited length (theoretically up to about 231 characters, about 2000 million).

Note that Unicode uses more than one byte to represent each character. As far as VB is concerned Unicode characters are two bytes which gives 216 or 65536 possible values. This is enough even for Chinese, Japanese and Korean character sets (CJK). In fact Unicode defines 17 planes each of which has room for 216 code points but VB (and Windows) uses only the Basic Multilingual Plane (BMP).

See What Are VB Strings? for a concise explanation of the internal workings of Visual Basic Classic strings.

Visual Basic provides a reasonable set of traditional functions for manipulating and searching strings. These functions are usually all that is needed for most programs that are not primarily concerned with text processing.

A regular expression is a character string in which some characters have special meanings. Such a string can be used to search for substrings in another string in much more sophisticated ways than the built in InStr function.

For example this expression:


will match either 'dog' or 'cat'.

Visual Basic has no built in regular expression functions, but these are available in the VBScript regular expression library. If your program does a lot of text processing regular expressions are definitely worth learning even though they may seem intimidating at the start. In practice most programmers find that the more arcane expressions are rarely used and the same idioms are recycled over and over so there is really not as much to learn as it at first appears.

Previous: Loops Contents Next: Built In String Functions