0% found this document useful (0 votes)
3 views

Strings Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Strings Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

How to get access to a SINGLE character in a string…

1. Consider the following string example:


sname := ‘Kishan Ramdayal’ ;
2. Each character in a string has a position value in that string.
i.e.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

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);

N.B. In this example of code, the result will be shown in a showmessage


dialog box as follows:
ANTOINETTE NAIDOO

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);

N.B. In this example of code, the result will be shown in a showmessage


dialog box as follows:
antoinette naidoo

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));

N.B. The result in this example in a showmessage dialog box would


be:
Length of string = 13

e.g. 2
//code snippet
//processing
ilen := length(‘Zenden Mudaly’);
//output
Showmessage(‘Length of string = ‘+intToStr(ilen));

N.B. The result in this example in a showmessage dialog box would


be:
Length of string = 13
The Copy() function/command
1. The copy() command extracts(copies) a smaller piece of string from a larger string.
2. Consider the following string:

sSentence := ‘I love food.’ ;


N.B. Remember that every character in a string has a position value.
_______________________________________________________________________________________________
e.g. 1
//code snippet
sPart1 := copy(sSentence, 1, 4);

from where? from what position? number of characters

//output
Showmessage(sPart1);

N.B. The above result would be shown in a showmessage as follows:

I lo
first 4 characters of sSentence
_______________________________________________________________________________________________
e.g. 2

//code snippet
sPart2 := copy(sSentence, 4, 2);

from where? from what position? number of characters

//output
Showmessage(sPart2);

N.B. The above result would be shown in a showmessage as follows:

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);

N.B. The above result would be shown in a showmessage as follows:

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.

3. Consider the following string:

sname := ‘Michael Mundie’ ;


_________________________________________________________________________
e.g.1
If I want to know what position the character ‘a’ in sname is, I will code as
follows:

//code snippet on the string above i.e. sname


//processing
ipos := pos(‘a’ , sname);

what you looking for… from where?

//output
Showmessage(‘The character a is in position: ‘ + intToStr(ipos));

N.B. The above output in a showmessage would be:


5
Outputs the position of the first occurrence
of the character. In this case, there is only
1 occurrence of the letter a.
_______________________________________________________________________________________
e.g.2
If I want to know what position the consecutive characters ‘und’ in sname is, I
will code as follows:

//code snippet on the string above i.e. sname


//processing
ipos := pos(‘und’ , sname);

//output
Showmessage(‘The pattern of characters und is in position: ‘ + intToStr(ipos));

N.B. The above output in a showmessage would be as follows:


10
The output will show the position of the first occurrence
of the pattern of characters. It will result in the
position value of the first character of the
pattern it finds.
The Delete() function/command

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:

sSentence := ‘I love food.’ ;


N.B. Remember that every character in a string has a position value.

_______________________________________________________________________________________________

e.g. 1
//code snippet on original sSentence
Delete(sSentence, 1, 4);

from where? from what position? number of characters

//output
Showmessage(sSentence);

N.B. The above result would be shown in a showmessage as follows:

ve food.
first 4 characters of sSentence deleted

_______________________________________________________________________________________________

e.g. 2

//code snippet on original sSentence


Delete(sSentence, 4, 2);

from where? from what position? number of characters

//output
Showmessage(sSentence);

N.B. The above result would be shown in a showmessage as follows:

I le food.
deletes only 2 characters from the 4th character
of sSentence

“AIM FOR EXCELLENCE”…ECM

You might also like