Visual Basic And Working With String Programming Computer Science Essay

Published:

Visual Basic (VB) is the third-generation event-driven programming language and integrated development environment (IDE) from Microsoft for its COM programming model. Visual Basic is relatively easy to learn and use

Like the BASIC programming language, Visual Basic was designed to be easily learned and used by beginner programmers. The language not only allows programmers to create simple GUI applications, but can also develop complex applications. Programming in VB is a combination of visually arranging components or controls on a form, specifying attributes and actions of those components, and writing additional lines of code for more functionality. Since default attributes and actions are defined for the components, a simple program can be created without the programmer having to write many lines of code. Performance problems were experienced by earlier versions, but with faster computers and native code compilation this has become less of an issue.

Although programs can be compiled into native code executables from version 5 onwards, they still require the presence of runtime libraries of approximately 1 MB in size. This runtime is included by default in Windows 2000 and later, but for earlier versions of Windows like 95/98/NT it must be distributed together with the executable.

Lady using a tablet
Lady using a tablet

Professional

Essay Writers

Lady Using Tablet

Get your grade
or your money back

using our Essay Writing Service!

Essay Writing Service

Forms are created using drag-and-drop techniques. A tool is used to place controls (e.g., text boxes, buttons, etc.) on the form (window). Controls have attributes and event handlers associated with them. Default values are provided when the control is created, but may be changed by the programmer. Many attribute values can be modified during run time based on user actions or changes in the environment, providing a dynamic application. For example, code can be inserted into the form resize event handler to reposition a control so that it remains centered on the form, expands to fill up the form, etc. By inserting code into the event handler for a keypress in a text box, the program can automatically translate the case of the text being entered, or even prevent certain characters from being inserted.

Visual Basic can create executables (EXE files), ActiveX controls, or DLL files, but is primarily used to develop Windows applications and to interface database systems. Dialog boxes with less functionality can be used to provide pop-up capabilities. Controls provide the basic functionality of the application, while programmers can insert additional logic within the appropriate event handlers. For example, a drop-down combination box will automatically display its list and allow the user to select any element. An event handler is called when an item is selected, which can then execute additional code created by the programmer to perform some action based on which element was selected, such as populating a related list.

Alternatively, a Visual Basic component can have no user interface, and instead provide ActiveX objects to other programs via Component Object Model (COM). This allows for server-side processing or an add-in module.

The language is garbage collected using reference counting, has a large library of utility objects, and has basic object oriented support. Since the more common components are included in the default project template, the programmer seldom needs to specify additional libraries. Unlike many other programming languages, Visual Basic is generally not case sensitive, although it will transform keywords into a standard case configuration and force the case of variable names to conform to the case of the entry within the symbol table. String comparisons are case sensitive by default, but can be made case insensitive if so desired.

The Visual Basic compiler is shared with other Visual Studio languages (C, C++), but restrictions in the IDE do not allow the creation of some targets (Windows model DLLs) and threading models.

String Functions

Strings are not objects so they do not have methods but there is a number of functions that manipulate strings. Note that none of the functions modify the original string, except for Mid$ when it is on the left hand side of an assignment statement:

Asc

Returns the integer code of the first character of the string. The inverse function would be Chr.

Len

Returns the length of the string.

InStr

Returns the character index of the first occurrence of the substring in a string or zero if the substring is not found.

Lady using a tablet
Lady using a tablet

Comprehensive

Writing Services

Lady Using Tablet

Plagiarism-free
Always on Time

Marked to Standard

Order Now

InstrB

Like InStr except that it returns the byte position. It has to be remembered, that Visual Basic 6 strings are Unicode strings.

InstrRev

Like InStr except that it returns the character position of the last occurrence of the substring.

Left$

Returns the specified number of characters from the beginning of the string. If there are fewer characters in the string Left$ returns the whole string, no error is raised,

Mid$

Returns a number of characters starting at the given position, on the left hand side it replaces those characters,

Right$

Returns the specified number of characters from the end of the string, if there are not that many characters, then Right$ returns the whole string.

IsNumeric

Returns true if the string looks like a number.

LTrim$, RTrim$, Trim$

Returns a copy of the string with leading, trailing or leading and trailing spaces removed respectively. Note that only ASCII spaces (character code 32) are removed, other whitespace characters such as tabs are treated as non-spaces.

LCase$, Ucase

Converts the whole string to lower case or upper case respectively.

Val

Returns a number corresponding to the number found at the start of the string. Note that Val is not locale aware, which means that it always expects decimal points regardless of the regional settings of your computer; if you are reading from a comma delimited file this is probably the function you want to use.

Str

Returns a string corresponding to the given number. Like Val this is not locale aware. This is the function you should use if you are creating a text file containing numbers to be read on someone else's computer.

CStr

Converts the expression to a string. This procedure is locale aware and the correct function to use if converting numbers and differently typed values to strings for user-display. Usually it is unnecessary because Visual Basic automatically converts when necessary and uses Regional Settings to do so.

Format$

Converts a number to a string using a specific format. The format is provided as a string of characters, that shows how many digits should be given before and after the decimal point. Like CStr, Format$ is locale aware so the decimal separator will be whatever is specified in the user's Regional Settings. Format$ also provides for conversion of dates to various built-in and custom string formats.

CBool, CByte, CCur, CInt, CLng, CSng, CDbl, CDec

Locale aware conversions to Boolean, Byte, Currency, Integer, Long, Single, Double, Decimal.

Split

Chops a string into pieces and returns a Variant Array. If no delimiter is specified then spaces will be used. Delimiters may be any string of any length. Two adjacent delimiters delimit an empty string.

Hex$

Returns a string of Hex characters representing a number.

Oct$

Returns a string of Octal characters representing a number.

Replace$

Returns a string with occurrences of a specified substring replaced with a new string. Note that the substring and the new string do not have to be the same size.

StrComp

Returns -1 if the first string is less than the second, 0 if they are identical, +1 if the first is greater than the second. Takes an optional argument that determines the comparison algorithm: vbBinary for exact comparisons using the character codes, vbTextCompare for case insensitive comparisons.

EXAMPLE:

Function:

Len

Description:

Returns a Long containing the length of the specified string

Syntax:

Len(string)

Where string is the string whose length (number of characters) is to be returned.

Example:

lngLen = Len("Visual Basic")    ' lngLen = 12

Function:

Mid$ (or Mid)

Description:

Returns a substring containing a specified number of characters from a string.

Syntax:

Mid$(string, start[, length])

The Mid$ function syntax has these parts:

string Required. String expression from which characters are returned.

start Required; Long. Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string ("").

Lady using a tablet
Lady using a tablet

This Essay is

a Student's Work

Lady Using Tablet

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Examples of our work

length Optional; Long. Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.

 

Example:

 

strSubstr = Mid$("Visual Basic", 3, 4)          ' strSubstr = "sual"

Note: Mid$ can also be used on the left side of an assignment statement, where you can replace a substring within a string.

strTest = "Visual Basic"

Mid$(strTest, 3, 4) = "xxxx"

 

'strTest now contains "Vixxxx Basic"

In VB6, the Replace$ function was introduced, which can also be used to replace characters within a string.

Function:

Left$ (or Left)

Description:

Returns a substring containing a specified number of characters from the beginning (left side) of a string.

Syntax:

Left$(string, length)

The Left$ function syntax has these parts:

string Required. String expression from which the leftmost characters are returned.

length Required; Long. Numeric expression indicating how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.

 

Example:

strSubstr = Left$("Visual Basic", 3)      ' strSubstr = "Vis"

 

' Note that the same thing could be accomplished with Mid$:

strSubstr = Mid$("Visual Basic", 1, 3)

Function:

Right$ (or Right)

Description:

Returns a substring containing a specified number of characters from the end (right side) of a string.

Syntax:

Right$(string, length)

The Right$ function syntax has these parts:

string Required. String expression from which the rightmost characters are returned.

length Required; Long. Numeric expression indicating how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.

Example:

strSubstr = Right$("Visual Basic", 3)     ' strSubstr = "sic"

 

' Note that the same thing could be accomplished with Mid$:

strSubstr = Mid$("Visual Basic", 10, 3)

Function:

UCase$ (or UCase)

Description:

Converts all lowercase letters in a string to uppercase. Any existing uppercase letters and non-alpha characters remain unchanged.

Syntax:

UCase$(string)

Example:

strNew = UCase$("Visual Basic")           ' strNew = "VISUAL BASIC"

Function:

LCase$ (or LCase)

Description:

Converts all uppercase letters in a string to lowercase. Any existing lowercase letters and non-alpha characters remain unchanged.

Syntax:

LCase$(string)

Example:

strNew = LCase$("Visual Basic")           ' strNew = "visual basic"

Function:

Instr

Description:

Returns a Long specifying the position of one string within another. The search starts either at the first character position or at the position specified by the start argument, and proceeds forward toward the end of the string (stopping when either string2 is found or when the end of the string1 is reached).

Syntax:

InStr([start,] string1, string2 [, compare])

The InStr function syntax has these parts:

start Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. The start argument is required if compare is specified.

string1 Required. String expression being searched.

string2 Required. String expression sought.

compare Optional; numeric. A value of 0 (the default) specifies a binary (case-sensitive) search. A value of 1 specifies a textual (case-insensitive) search.

Examples:

lngPos = Instr("Visual Basic", "a")

' lngPos = 5

 

lngPos = Instr(6, "Visual Basic", "a")

' lngPos = 9       (starting at position 6)

 

lngPos = Instr("Visual Basic", "A")

' lngPos = 0       (case-sensitive search)

 

lngPos = Instr(1, "Visual Basic", "A", 1)

' lngPos = 5       (case-insensitive search)

Function:

InstrRev

Description:

Returns a Long specifying the position of one string within another. The search starts either at the last character position or at the position specified by the start argument, and proceeds backward toward the beginning of the string (stopping when either string2 is found or when the beginning of the string1 is reached).

Introduced in VB 6.

Syntax:

InStrRev(string1, string2[, start, [, compare]])

The InStr function syntax has these parts:

string1 Required. String expression being searched.

string2 Required. String expression sought.

start Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the last character position.

compare Optional; numeric. A value of 0 (the default) specifies a binary (case-sensitive) search. A value of 1 specifies a textual (case-insensitive) search.

Examples:

lngPos = InstrRev("Visual Basic", "a")

' lngPos = 9

 

lngPos = InstrRev("Visual Basic", "a", 6)

' lngPos = 5 (starting         at position 6)

 

lngPos =       InstrRev("Visual Basic", "A")

' lngPos = 0         (case-sensitive search)

 

lngPos =       InstrRev("Visual Basic", "A", , 1)

' lngPos = 9         (case-insensitive search)

' Note         that this last example leaves a placeholder for the start argument

Function:

InstrRev

Description:

Returns a Long specifying the position of one string within another. The search starts either at the last character position or at the position specified by the start argument, and proceeds backward toward the beginning of the string (stopping when either string2 is found or when the beginning of the string1 is reached).

Introduced in VB 6.

Syntax:

InStrRev(string1, string2[, start, [, compare]])

The InStr function syntax has these parts:

string1 Required. String expression being searched.

string2 Required. String expression sought.

start Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the last character position.

compare Optional; numeric. A value of 0 (the default) specifies a binary (case-sensitive) search. A value of 1 specifies a textual (case-insensitive) search.

Examples:

lngPos = InstrRev("Visual Basic", "a")

' lngPos = 9

 

lngPos = InstrRev("Visual Basic", "a", 6)

' lngPos = 5 (starting         at position 6)

 

lngPos =       InstrRev("Visual Basic", "A")

' lngPos = 0         (case-sensitive search)

 

lngPos =       InstrRev("Visual Basic", "A", , 1)

' lngPos = 9         (case-insensitive search)

' Note         that this last example leaves a placeholder for the start argument

Function:

Space$ (or Space)

Description:

Returns a string containing the specified number of blank spaces.

Syntax:

Space$(number)

Where number is the number of blank spaces desired.

 

Examples:

strTest = Space$(5)     ' strTest = "     "

Function:

Replace$ (or Replace)

Description:

Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Introduced in VB 6.

Syntax:

Replace$(expression, find, replacewith[, start[, count[, compare]]])

The Replace$ function syntax has these parts:

expression Required. String expression containing substring to replace.

find Required. Substring being searched for.

replacewith Required. Replacement substring.

start Optional. Position within expression where substring search is to begin. If omitted, 1 is assumed.

count Optional. Number of substring substitutions to perform. If omitted, the default value is â€"1, which means make all possible substitutions.

compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. (0 = case sensitive, 1 = case-insensitive)

Built-in "vb" constants can be used for the compare argument:

vbBinaryCompare for 0 (case-sensitive search)

vbTextCompare for 1 (case-insensitive search)

Examples:

strNewDate = Replace$("08/31/2001", "/", "-")

' strNewDate = "08-31-2001"

Function:

StrReverse$ (or StrReverse)

Description:

Returns a string in which the character order of a specified string is reversed.

Introduced in VB 6.

Syntax:

StrReverse$(string)

Examples:

strTest = StrReverse$("Visual Basic")           ' strTest = "cisaBlausiV"

 

Function:

LTrim$ (or LTrim)

Description:

Removes leading blank spaces from a string.

Syntax:

LTrim$(string)

Examples:

strTest =         LTrim$("  Visual Basic  ")

' strTest =         "Visual Basic  "

Function:

RTrim$ (or RTrim)

Description:

Removes trailing blank spaces from a string.

Syntax:

RTrim$(string)

Examples:

strTest = RTrim$("Visual Basic")      ' strTest = "Visual Basic"

Function:

Trim$ (or Trim)

Description:

Removes both leading and trailing blank spaces from a string.

Syntax:

Trim$(string)

Examples:

strTest = Trim$("  Visual Basic  ")       ' strTest = "Visual Basic"

' Note: Trim$(x) accomplishes the same thing as LTrim$(RTrim$(x))

Function:

Asc

Description:

Returns an Integer representing the ASCII character code corresponding to the first letter in a string.

Syntax:

Asc(string)

Examples:

intCode = Asc("*")      ' intCode = 42

intCode = Asc("ABC")    ' intCode = 65

Function:

Chr$ (or Chr)

Description:

Returns a string containing the character associated with the specified character code.

Syntax:

Chr$(charcode)

Where charcode is a number from 0 to 255 that identifies the character.

Examples:

strChar = Chr$(65)     ' strChar = "A"