Strings Notes
Strings Notes
Kishan Ramdayal
Further explanation:
K is in position 1
n is in position 6
The space is in position 7
R is in position 8
The first a in Kishan is in position 5
etc…
3. To access any character at any time for coding purposes, use the string variable
followed by square brackets [] and the position you looking for within the brackets.
e.g. 1
//code snippet based on the name stored in the variable sname above
Showmessage(sname[2]) ;
N.B. The result of the above would be in a showmessage dialog box as follows:
i
e.g.2
//code snippet based on the name stored in the variable sname above
Showmessage(sname[9]) ;
N.B. The result of the above would be in a showmessage dialog box as follows:
a
e.g.3
//code snippet based on the name stored in the variable sname above
//in this example, 2 letters concatenated(joined)
Showmessage(sname[1] + sname[10]) ;
N.B. The result of the above would be in a showmessage dialog box as follows:
Km
UpperCase/LowerCase/UpCase string Functions
1. UpperCase() Function
This function changes the case of letters in a string to capital/uppercase
letters.
e.g.
//code snippet
//input and processing
sname := ‘Antoinette Naidoo’;
sname:= UpperCase(sname);
//output
Showmessage(sname);
2. LowerCase() Function
This function changes the case of letters in a string to small/lowercase
letters.
e.g.
//code snippet
//input and processing
sname := ‘Antoinette Naidoo’;
sname:= LowerCase(sname);
//output
Showmessage(sname);
3. Upcase()
N.B. This function changes the case of a single character BUT will be
discussed later on.
STRING HANDLING BASIC EXAMPLES
1. Length() Function
This function finds the length of a string.
It returns an integer value for the length of the string.
It counts every character including spaces that is stored in a string
variable or even the string directly.
e.g. 1
//code snippet
//processing
sname := ‘Zenden Mudaly’ ;
ilen := length(sname);
//output
Showmessage(‘Length of string = ‘+intToStr(ilen));
e.g. 2
//code snippet
//processing
ilen := length(‘Zenden Mudaly’);
//output
Showmessage(‘Length of string = ‘+intToStr(ilen));
//output
Showmessage(sPart1);
I lo
first 4 characters of sSentence
_______________________________________________________________________________________________
e.g. 2
//code snippet
sPart2 := copy(sSentence, 4, 2);
//output
Showmessage(sPart2);
ov
copying/extracting only 2 characters from the 4th character
of sSentence
_______________________________________________________________________________________________
e.g. 3
//code snippet
sPart3 := copy(sSentence, 5);
from where? from what position? …Note: copies from this position until the end of the string
//output
Showmessage(sPart3);
ve food.
copying/extracting from the 5th character until the end of sSentence
The pos() Function
1. The pos() function (pos is short for position) finds the position of the FIRST occurrence of a
character or a pattern of characters in a string.
2. This function’s result is an integer value for the position where the character/pattern is found in
the string. Therefore the result must be stored in an integer variable.
//output
Showmessage(‘The character a is in position: ‘ + intToStr(ipos));
//output
Showmessage(‘The pattern of characters und is in position: ‘ + intToStr(ipos));
1. The delete() command deletes/removes a smaller piece of string from a larger string.
2. The delete() function result is not assigned to a variable because it just changes the state of the original
string.
3. Consider the following string:
_______________________________________________________________________________________________
e.g. 1
//code snippet on original sSentence
Delete(sSentence, 1, 4);
//output
Showmessage(sSentence);
ve food.
first 4 characters of sSentence deleted
_______________________________________________________________________________________________
e.g. 2
//output
Showmessage(sSentence);
I le food.
deletes only 2 characters from the 4th character
of sSentence