String Handling

Description and exemplification of the following constructs in pseudocode and an appropriate high level language: string operations (concatenation and substrings)

String and string handling are the bread and butter of all programs.  Today we looked at how the programmer can cut (substring) and paste (concatenation) strings together.

  • Len – Output is the length of the string
  • Ucase – Output is the string in upper case
  • Lcase – Output is the string in lower case
  • Asc – Output is the ASCII value of the character
  • Chr – Output is the ASCII character of the value
  • Mid$ – outputs the characters from anypoint in the string.
    • Mid$(String,First,HowMany)
  • Strings.Left – Outputs the leftmost characters in a string.
    • Strings.Left(String,HowMany)
  • Strings.Right – Outputs the rightmost characters in a string.
    • Strings.Right(String,HowMany)

Concatenation is a scary-looking word, but it simply means “joining together”.  String concatenation means joining two (or more) strings together to form one new string: String concatenation in VB is very simple – you don’t need a special function – you just use the + sign.

For example,

  • if string1 = “Fred”,
  • and string2 = “McSporran”,
  • then string1 + string2 = “FredMcSporran”.

Today’s “Impossible Task”

Create a program that displays the ASCii codes of all the characters of a string that is entered by the user, solution after the jump.

Please ignore the bad formatting and bullet points, there is no code plugin installed within Glow.
  • Dim StrMessage As String
  • Dim AsciiMessage As String
  • Dim StrLetter As String
  • Dim intLength As Integer
  • Dim Position As Integer
  • AsciiMessage = “”
  • LstOutput.Items.Clear()
  • StrMessage = InputBox(“Please enter the string to convert”)
  • intLength = Len(StrMessage)
  • For Position = 1 To intLength
    • StrLetter = Mid$(StrMessage, Position, 1)
    • LstOutput.Items.Add(Asc(StrLetter))
    • AsciiMessage = AsciiMessage + ” ” + CType(Asc(StrLetter), String)
  • Next
  • MsgBox(AsciiMessage)