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

FunctionDescriptionExample
Asc(string)Returns the character code of (the first letter of) a stringintCode = Asc("A")
Chr(integer)Returns the Char associated with the integer codemyChar = Chr(65)
GetChar(string , n)Returns the Char in position n (starting from 0) in the given stringmyStr = GetChar(inputString, 4)
InStr(string1, string2)Returns the start position of the first occurrence of string2 in string 1intPos = InStr(check, match)
InStrRev(string1, string2)Returns the start position of the first occurrence of string2 in string 1 working from right to leftintPos = InStrRev(check, match)
LCase(string)Returns the string converted to lower casemyStr = LCase("ShfYIhf")
Left(string, n)Returns a string made up of the first n characters of the input stringmyStr = Left("abcdef", 3)
Len(string)Returns the length of the input stringintL = Len("shfdhjfj")
LTrim(string)Returns a string with white space removed from the left hand sidemyStr = LTrim(" jjj")
Mid(string, start, length)Returns a string from within the input string with the specified start position and lengthmyStr = 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 substringmyStr = 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 stringmyStr = Right("abcdef", 3)
RTrim(string)Returns a string with white space removed from the right hand sidemyStr = RTrim("jjj ")
Space(n)Returns a string consisting of n spacesmyStr = "Hello" + Space(2)
StrReverse(string)Returns the reverse of the given stringmyStr = StrReverse("OHM")
Trim(string)Returns a string with white space removed from both sidesmyStr = Trim(" jjj ")
UCase(string)Returns the input string converted to upper casemyStr = 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.

MethodDescriptionExample
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

  1. Read in a string and work out how many characters it contains and then convert the entire string to upper case.
  2. 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!
  3. Write a program to read in a word and test if it is a palindrome (reads the same forwards as it does backwards).