Strings
Strings are made for holding text.
Example:
SCAR Code:
var
myString: String;
begin
myString := 'This is a pascal string!';
end;
Big word alert: Concatenation
Concatenation is the combination of one or more strings to make one longer string.
Strings can also be concatenated together (connected) with the + symbol.
SCAR Code:
WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
or
SCAR Code:
var
str1, str2: String;
begin
str1 := 'A';
str2 := 'B';
WriteLn(str1+str2); // <- here
end;
Sometimes you need to compare two strings to see if they are alike. In these situations you don't always know the casing of each string.
Say someone provided a variable with the first letter capitalized when you hadn't intended for it.
SCAR Code:
Loc[2] := 'Bank'; //Supposed to be 'bank'
If you try to compare the two,
SCAR Code:
if('bank' = 'Bank') then
WriteLn('true');
You will find out they are not equal.
To fix this problem you should always convert both strings you are about to compare like such.
SCAR Code:
if(LowerCase('bank') = LowerCase('Bank')) then
WriteLn('true');
There are some built in functions used for extracting information from strings. These will return (as a String) the information specified by the function. Some examples are...
GetNumbers(Text: string): string;
SCAR Code:
WriteLn(GetNumbers('AB162hdfh3')); //Will return '1623'
GetNumbers(Text: string): string;
SCAR Code:
WriteLn(GetLetters('123A456B')); //Will return 'AB'
GetOthers(Text: string): string;
SCAR Code:
WriteLn(GetOthers('abcd12345!@#$%'));//Will return '!@#$%''
Some functions are used to omit information from Strings. Some examples of these are...
TrimNumbers(Text: string): string;
SCAR Code:
WriteLn(TrimNumbers('abcd12345'));//Will return 'abcd''
TrimLetters(Text: string): string;
SCAR Code:
WriteLn(TrimLetters('abcd12345'));//Will return '12345''
TrimOthers(Text: string): string;
SCAR Code:
WriteLn(TrimOthers('a!b@c#d$%'));//Will return 'abcd''
Sometimes you need to grab a segment of a string.
copy(s: string; ifrom, icount: LongInt): string;
SCAR Code:
copy('chimp', 1, 5); // would return 'chimp'
copy('chimp', 1, 4); // would return 'chim'
copy('chimp', 2, 5); // would return 'himp'
And this is the end of my short strings tutorial. If I think of anything else that should be added I will add it in. Hopefully some new scripters learn a thing or two about Strings.