Topic: 2.3.5 Built-In Functions: Location

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Computer Science 9608 (Notes)

Chapter: 2.3 Programming

Topic: 2.3.5 Built-in functions


Location

Many programming languages allow the searching for a short string of characters within a longer string.
This would be similar to searching this book for the word ‘computer’.

Common Key words for finding the location of one string within another are LOCATE, FIND and POSITION

Extraction

At times, a programmer will only want part of a string. Any part of a string can be obtained using the
RIGHT, LEFT and MID functions.

The following string manipulation functions are called by typing:

Result = FunctionName(String [,x,y])

Page 1 of 7
Computer Science 9608 (Notes)
Chapter: 2.3 Programming

Topic: 2.3.5 Built-in functions


Concatenation

Concatenation is where two or more strings are joined together to make a single string.

Note that when two strings are added together the second string is added to the end of the first:

“Peter” + “Jones” = “PeterJones”


“Peter” + “ “ + “Jones” = “Peter Jones”
“36.85” + “47” = “36.8547”
“47” + “36.85” =“4736.85”
“3/10/03” + “15” = “3/10/0315”

Length

Sometimes a programmer will need to know how many characters there are in a string.

Conversion

Strings and numbers

Strings are a sequence of ASCII characters, even if they contain only numbers such as “241”, and so they
cannot be used within an arithmetic calculation – they need to be ‘evaluated’ first.

Likewise a number such as 27.3 is not stored as ASCII characters and so cannot be used within any of the
string functions (such as Left, Right, Mid).

The function STR converts a number into a string and VAL converts a string into a number:

Page 2 of 7
Computer Science 9608 (Notes)
Chapter: 2.3 Programming

Topic: 2.3.5 Built-in functions


Characters and ASCII codes

The following two functions are used to either find the ASCII code for a specific character or to find the
character from the ASCII code:

ASCII character codes

Below is a table showing the most common characters and their ASCII codes:

Page 3 of 7
Computer Science 9608 (Notes)
Chapter: 2.3 Programming

Topic: 2.3.5 Built-in functions


Note the following:

the codes less than 32 are ‘control’ codes that are not used in strings;
‘space’ (code 32) has the lowest code;
next comes most of the punctuation symbols;
then digits 0–9
then uppercase letters
then lowercase letters
all other characters (e.g. é, å, π, etc.) have codes higher than these.

Comparing strings

When comparing strings, the codes of each string are compared, character by character, to decide which
string is greater.

Because it is the ASCII codes that are compared the following applies:

“Computing” <> “computing”

in fact:
“Computing” < “computing”
“10” < “2”
“02” < “1”
“1 120” < “1,120”
“ computing” < “Computing”

Page 4 of 7
Computer Science 9608 (Notes)
Chapter: 2.3 Programming

Topic: 2.3.5 Built-in functions


Sorting filenames

The following table shows a list of inconsistently names music tracks and the order in which they would
be sorted:

In order to get them sorted into the required order, they must be renamed with consistent use of
uppercase letters, spaces and leading zeros.

Recommended naming would be:

Track 01.mp3, Track 02.mp3, …, Track 10.mp3, Track 11.mp, …

Output data onto screen/file/printer, formatting the data for output as necessary.

Output will be either to the screen, a file or the printer.

Page 5 of 7
Computer Science 9608 (Notes)
Chapter: 2.3 Programming

Topic: 2.3.5 Built-in functions


Output controls

Output to the screen could be via a dialogue/message box, a text box, a list box or simply direct on the
form.

Custom string formatting can be accomplished using specific characters recognized by the Format$
function, shown in the table below:

Page 6 of 7
Computer Science 9608 (Notes)
Chapter: 2.3 Programming

Topic: 2.3.5 Built-in functions


To demonstrate custom string formats using combinations of the characters listed above, set up another
"Try It" project, and place the following code in the cmdTryIt_Click event of cmdTryIt button:

Private Sub cmdTryIt_Click()


Dim strName As String
strName = InputBox("Please enter your name:")

Print "Using '>':"; Tab(25); Format$(strName, ">")


Print "Using '<':"; Tab(25); Format$(strName, "<")
Print "Using '@':"; Tab(25); "Hello there, "; Format$(strName, "@@@@@@@@@@"); ". How are you?"
Print "Using '&':"; Tab(25); "Hello there, "; Format$(strName, "&&&&&&&&&&"); ". How are you?"
Print "Using '!@':"; Tab(25); "Hello there, "; Format$(strName, "!@@@@@@@@@@"); ". How are you?"
Print "Using '!&':"; Tab(25); "Hello there, "; Format$(strName, "!&&&&&&&&&&"); ". How are you?"
Print "Using '\':"; Tab(25); Format$(strName, "\H\e\l\l\o\,\ &&&&&&&&&&\.")
Print "Using embedded quotes:"; Tab(25); Format$(strName, """Hello, ""&&&&&&&&&&"".""")

End Sub

Run the project and click the "Try It" button. When the input box appears, enter a name in mixed case (in
this example, Bruce was entered). The strings will be displayed as follows:

Page 7 of 7

You might also like