Visual Basic 2010 (Console) Guide
String Handling
There are a series of built-in functions and constants in the Microsoft Visual Basic Run-Time Library. The strings module of this library contains many useful functions for doing things with strings,
Visual Basic Run-Time Library Functions
Function | Description | Example |
---|---|---|
Asc(string) | Returns the character code of (the first letter of) a string | intCode = Asc("A") |
Chr(integer) | Returns the Char associated with the integer code | myChar = Chr(65) |
GetChar(string , n) | Returns the Char in position n (starting from 0) in the given string | myStr = GetChar(inputString, 4) |
InStr(string1, string2) | Returns the start position of the first occurrence of string2 in string 1 | intPos = InStr(check, match) |
InStrRev(string1, string2) | Returns the start position of the first occurrence of string2 in string 1 working from right to left | intPos = InStrRev(check, match) |
LCase(string) | Returns the string converted to lower case | myStr = LCase("ShfYIhf") |
Left(string, n) | Returns a string made up of the first n characters of the input string | myStr = Left("abcdef", 3) |
Len(string) | Returns the length of the input string | intL = Len("shfdhjfj") |
LTrim(string) | Returns a string with white space removed from the left hand side | myStr = LTrim(" jjj") |
Mid(string, start, length) | Returns a string from within the input string with the specified start position and length | myStr = Mid(inputStr, 3, 2) |
Replace(search, find, replace) | Returns a string based on the input string search, with all instances of the substring find replaced with the specified new substring | myStr = Replace(inputStr, "he", "she") |
Right(string, n) | Returns a string made up of the first n characters from the right hand side of the input string | myStr = Right("abcdef", 3) |
RTrim(string) | Returns a string with white space removed from the right hand side | myStr = RTrim("jjj ") |
Space(n) | Returns a string consisting of n spaces | myStr = "Hello" + Space(2) |
StrReverse(string) | Returns the reverse of the given string | myStr = StrReverse("OHM") |
Trim(string) | Returns a string with white space removed from both sides | myStr = Trim(" jjj ") |
UCase(string) | Returns the input string converted to upper case | myStr = UCase("jhjhj") |
String Class Methods
The following methods are part of the String class and have some additional functionality. Remember that Visual Studio is part of a family of languages that use a common framework. The String class is a .NET class and, as such, its members are accessible whichever .NET language you use. Manipulating strings using the methods of the string class is likely to provide an experience closer to that you will find with other languages.
Method | Description | Example |
---|---|---|
string.Concat(a, b) | Concatenates (adds together) the two strings a and b returning a new string. | c = string.Concat(a,b); |
Contains(a) | Returns true or false depending on whether the string contains the string value specified by the parameter a. | inString = myString.Contains("@"); |
IndexOf(a) | Returns an integer representing the position of the first occurrence of the string or character a. | pos = myString.IndexOf("@"); |
LastIndexOf(a) | Returns an integer representing the position of the last occurrence of the string or character a. | pos = myString.LastIndexOf("@"); |
Remove(a) | Removes all of the characters of the string beginning at position a and continuing through to the end. | newString = myString.Remove(3); |
Remove(a,b) | Removes b characters from the string starting at position a. | newString = myString.Remove(3, 2); |
Replace(a,b) | Replaces all instances of the string a with the string b. | newString = myString.Replace("his", "her"); |
Substring(a) | Returns a substring from the string, starting at position a. | mySub = myString.Substring(3); |
Substring(a,b) | Returns a substring from the string, starting at position a and continuing for b characters. | mySub = myString.Substring(3, 4); |
ToLower() | Copies the string to lowercase. | newString = myString.ToLower(); |
ToUpper() | Copies the string to uppercase. | newString = myString.ToUpper(); |
Trim() | Removes white space characters from the start and end of a string. | newString = myString.Trim(); |
TrimEnd() | Removes white space characters from the end of a string. | newString = myString.TrimEnd(); |
TrimStart() | Removes white space characters from the start of the string. | newString = myString.TrimStart(); |
Having A Go
- Read in a string and work out how many characters it contains and then convert the entire string to upper case.
- Set a string to the first line of the outrageous ditty below. Search for each of the spaces within the first line and use the Remove method to produce the whole rhyme.
OH SIR JASPER DO NOT TOUCH ME!
OH SIR JASPER DO NOT TOUCH!
OH SIR JASPER DO NOT!
OH SIR JASPER DO!
OH SIR JASPER!
OH SIR!
OH! - Write a program to read in a word and test if it is a palindrome (reads the same forwards as it does backwards).