All About Visual Basic 6 Programming

String Related Functions

Posted by Psycho Programer On Friday, December 10, 2010 0 comments

Visual Basic 6.0 has various string handling functions for different string related operations. These functions can be used to solve different string related problems without using loops. Some of the string related functions are listed below:

Function

Description

Return Type

Len (String) Returns the Length of the string. Integer
LenB (String) Returns the number of bytes in a string. Integer
Asc (String) Returns the ASCII value of the first character in String. Integer
Chr (Integer) Returns a String with the corresponding ASCII code. String
Left$ (String, x)  Left (String, x) Returns x characters from the start of String. String
Right$ (String,x)
Right (String, x)
Returns x characters from the end of String. String
Mid$(String, x, y)
Mid (String, x, y)
Returns y characters from String, starting at the x'th character. String
Cstr (x) Returns the string representation of x (localized). String
Str$ (x)
Str (x)
Returns the string representation of x (not localized). String
UCase$ (String)
UCase (String)
Coverts String to Upper Case. String
LCase$ (String)
LCase (String)
Converts String to Lower Case. String
InStr ([start,] string1, string2 [, compare]) Returns a Long specifying the position of one string within another. Long
InStrRev(string1, string2[, start, [, compare]]) Returns a Long specifying the position of one string within another from backward. Long
String$(number,character) Returns a string containing a repeating character string of the length specified. String
StrComp(String1, String2,
 Attribute)
Returns 0 if strings are equal, 1 if String1 sorts after String2 and –1 if String1 sorts ahead of String2. Integer
Replace$ (or Replace)(expression, find, replacewith[, start[, count[, compare]]]) Returns a string in which a specified substring has been replaced with another substring a specified number of times. String
StrReverse$ (string)
StrReverse (string)
Reverse the String. String
LTrim$ (String)
LTrim (String)
Removes leading blank spaces from a string. String
RTrim$ (String)
RTrim (String)
Removes trailing blank spaces from a string. String
Trim$ (String)
Trim (String)
Removes both leading and trailing blank spaces from a string. String
Split(expression[, delimiter[, count[, compare]]]) Splits a string into separate elements based on a delimiter (such as a comma or space) and stores the resulting elements in a zero-based array zero-based, one-dimensional array containing a specified number of substrings.
Join (list[, delimiter]) Joins (Concatenates) elements of an array into an output string. String
Filter(InputStrings, Value[, Include[, Compare]]) Returns a zero-based array containing subset of a string array based on a specified filter criteria. String

The Attribute Field of StrComp Function takes

Binary : Performs a binary comparison, based on a sort order derived from the internal binary representations of the characters.

Text : Performs a text comparison, based on a case-insensitive text sort order determined by your application's current culture information.

Try following piece of Code. This Function will Trim all spaces from the sentence.

 
   1:  Function TrimAllSpaces(data As String)
   2:  Dim str As String
   3:  Dim length As Integer
   4:  data = Trim(data)
   5:  length = Len(data)
   6:  For i = 1 To length Step 2
   7:      str = str & Trim(Mid(data, i, 2))
   8:  Next i
   9:  lblOutput.Caption = str
  10:  End Function

The 4th line data = Trim(data) trims all leading and trailing spaces and store the result to data. The 5th line stores the length of the string (data) to variable length. The 7th line str = str & Trim(Mid(data, i, 2)) extracts 2 characters from the ith position of the string, trim leading and trailing spaces and concatenate the result to variable str.

Call function TrimAllSpaces on Click event of Button Trim

   1:  Private Sub cmdTrim_Click()
   2:  TrimAllSpaces txtInput.Text
   3:  End Sub

Run the project , enter a string of your choice and click the "Trim" button.

The screen shot shows a run of the project using the code above.

Trim

Using Function UCase, LCase and StrReverse

Function to convert Lower Case to Upper Case

   1:  Function upperCase(data As String)
   2:  data = UCase(data)
   3:  lblOutput.Caption = data
   4:  End Function

Function to convert Upper Case to Lower Case

   1:  Function lowerCase(data As String)
   2:  data = LCase(data)
   3:  lblOutput.Caption = data
   4:  End Function

Function to reverse a string

   1:  Function stringReverse(data As String)
   2:  data = StrReverse(data)
   3:  lblOutput.Caption = data
   4:  End Function

0 comments:

Post a Comment