SRL Archive - Outdated Tutorials

Pascal Cheat Sheet - Data Types : Strings


ape

2010-07-05 12:32:39

Strings

Strings are made for holding text.
Example:

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.

WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
or

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.

Loc[2] := 'Bank'; //Supposed to be 'bank'
If you try to compare the two,

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.

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;

WriteLn(GetNumbers('AB162hdfh3')); //Will return '1623'
GetNumbers(Text: string): string;

WriteLn(GetLetters('123A456B')); //Will return 'AB'
GetOthers(Text: string): string;

WriteLn(GetOthers('abcd12345!@#$%'));//Will return '!@#$%''

Some functions are used to omit information from Strings. Some examples of these are…
TrimNumbers(Text: string): string;

WriteLn(TrimNumbers('abcd12345'));//Will return 'abcd''
TrimLetters(Text: string): string;

WriteLn(TrimLetters('abcd12345'));//Will return '12345''
TrimOthers(Text: string): string;

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;

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.

Shuttleu

2010-07-05 12:36:37

FIRST!

add the functions for removing spaces, numbers, letters etc

~shut

EDIT:
function GetNumbers(Text: string): string;
function GetLetters(Text: string): string;
function GetOthers(Text: string): string;
function TrimNumbers(Text: string): string;
function TrimLetters(Text: string): string;
function TrimOthers(Text: string): string;
function Capitalize(S: string): string;
function Uppercase(s: string): string;
function Lowercase(s: string): string;

ape

2010-07-05 12:39:21

From: Shuttleu
FIRST!

add the functions for removing spaces, numbers, letters etc

~shut

EDIT:
function GetNumbers(Text: string): string;
function GetLetters(Text: string): string;
function GetOthers(Text: string): string;
function TrimNumbers(Text: string): string;
function TrimLetters(Text: string): string;
function TrimOthers(Text: string): string;


Completely slipped my mind, will add them in…

HyperSecret

2010-07-05 12:39:52

May want to include what concatenation is. People don't use that word on a daily basis, and if they are new to programming would be clueless…

Try to stay constant with examples in tutorials. Since you compared 'bank' to 'Bank' with =, you should compare lowerCase('bank') to lowerCase('Bank') instead of throwing string literals in an example and then variables in. Makes things confusing for beginners.

Shuttleu

2010-07-05 12:43:38

maybe after a while you could explain all the string functions
function copy(s: string; ifrom, icount: LongInt): string;
Returns part of the string (Copy('abcde',2,3) would return 'bcd'.

function pos(substr, s: string): LongInt;
Returns position of substring in string. Returns 0 if not found.

procedure delete(var s: string; ifrom, icount: LongInt);
Delete part of string.

procedure insert(s: string; var s2: string; ipos: LongInt);
Insert s into s2.

function Between(s1, s2, s: string): string;
Returns part of string s between substrings s1 and s2.

function StrGet(var S: string; I: Integer): Char;

procedure StrSet(c: Char; I: Integer; var s: string);

function Uppercase(s: string): string;
Returns the specified string in upper case.

function Lowercase(s: string): string;
Returns the specified string in lowercase.

function Trim(s: string): string;
Removes spaces from start and end of string.

function Length(s: string): LongInt;
Returns string length in characters.

function Left(Text: string; Count: Integer): string;
Returns a part of the leftside of a string, the length is specified by Count.

function Right(Text: string; Count: Integer): string;
Returns a part of the rightside of a string, the length is specified by Count.

function Replace(Text, FindStr, ReplaceStr: string): string;
Replaces all instances of FindStr in Text by ReplaceStr.

function GetNumbers(Text: string): string;
Returns all numerical chars found in a string.

function GetLetters(Text: string): string;
Returns all alphabetical chars found in a string.

function GetOthers(Text: string): string;
Returns all non-numerical and non-alphabetical chars in a string.

function TrimNumbers(Text: string): string;
Removes all numerical chars from a string.

function TrimLetters(Text: string): string;
Removes all alphabetical chars from a string.

function TrimOthers(Text: string): string;
Removes all chars that are not numerical and not alphabetical from a string.

function Capitalize(S: string): string;
Will capitalize strings.
Example:
hello world => Hello World

function PosEx(search, s: string; from: Integer): Integer;
Returns the position of the substring in s searching forwards from from.

function LastPosEx(search, s: string; from: Integer): Integer;
Returns the position of the substring in s searching backwards from from.

function LastPos(search, s: string): Integer;
Returns the position of the substring in s searching backwards from the end of the string.

function StartsWith(prefix, s: string): Boolean;
Returns true if s starts with prefix.

function EndsWith(suffix, s: string): Boolean;
Returns true if s ends with suffix.

function TrimEx(delimiter, s: string): string;
Trims the specified delimiter from the start and end of the string.

function RegexPos(Txt, Regex: string): Integer;
Returns the position of the first found occurence of the regex.

function FindRegex(Txt, Regex: string): string;
Returns the string found by the regex.

function ReplaceRegex(Txt, Regex, ReplaceStr: string): string;
Replaces all occurences of the found regex by ReplaceStr.

function Format(s: string; data: array of const): string;
Formats a string with data.

function Implode(Glue: string; Pieces: TStringArray): string;
Implodes strings into 1 string.

function Explode(Separator, s: string): TStringArray;
Explodes a string into pieces.

function ExplodeEx(Separator, s: string; Limit: Integer): TStringArray;
Explodes a string into pieces with a Limit.

~shut

ape

2010-07-05 12:55:38

From: HyperSecret
May want to include what concatenation is. People don't use that word on a daily basis, and if they are new to programming would be clueless…

Try to stay constant with examples in tutorials. Since you compared 'bank' to 'Bank' with =, you should compare lowerCase('bank') to lowerCase('Bank') instead of throwing string literals in an example and then variables in. Makes things confusing for beginners.

I agree, I will fix this.


maybe after a while you could explain all the string functions

I will try to find the ones that are most useful as I was trying to keep this tutorial somewhat short and concise.

Boreas

2010-07-06 11:24:42

Not a big deal but this
WriteLn('String 1 '+'String 2'); // This will write: String 1  String 2 
has an extra space
WriteLn('String 1 '+' String 2'); // This will write: String 1  String 2
WriteLn('String 1'+' String 2'); // This will write: String 1 String 2
WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
WriteLn('String 1'+' String 2'); // This will write: String 1 String 2

ape

2010-07-06 15:12:20

From: Boreas
Not a big deal but this
WriteLn('String 1 '+'String 2'); // This will write: String 1  String 2 
has an extra space
WriteLn('String 1 '+' String 2'); // This will write: String 1  String 2
WriteLn('String 1'+' String 2'); // This will write: String 1 String 2
WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
WriteLn('String 1 '+'String 2'); // This will write: String 1 String 2
WriteLn('String 1'+' String 2'); // This will write: String 1 String 2


Ill fix that :), I originally had an extra space before String 2 but i forgot to delete it in the comments.

tom99

2010-07-10 12:30:49

Good guide, i didnt knew anything under this:

if(LowerCase('bank') = LowerCase('Bank')) then
WriteLn('true');


feel like that noob now xD, always nice to learn something new, but what is the copy command for? it does nothing is it supose to copy a string to clipboard?

i luffs yeww

2010-07-10 13:24:31

function copy(s: string; ifrom, icount: LongInt): string;
Returns part of the string (Copy('abcde',2,3) would return 'bcd'.

Seems pretty straight forward to me..

ape

2010-07-10 13:59:36

Copy has nothing to do with the clipboard, it is merely intended for copying specified segments of strings.

JPHamlett

2010-07-11 00:32:09

Nothing big but you have


GetNumbers(Text: string): string;

in there in instead of Getletters

BraK

2010-07-14 09:46:23

Had some interesting things in here. I haven't seen most of these function used mainstream I'll keep them in mind. I might just come across something that needs them sometime. Nice Tut

anonymity

2011-02-18 03:24:26

/BUMP!!!!

From: Shuttleu
function copy(s: string; ifrom, icount: LongInt): string;
Returns part of the string (Copy('abcde',2,3) would return 'bcd'.

function pos(substr, s: string): LongInt;
Returns position of substring in string. Returns 0 if not found.

procedure delete(var s: string; ifrom, icount: LongInt);
Delete part of string.

procedure insert(s: string; var s2: string; ipos: LongInt);
Insert s into s2.

function Between(s1, s2, s: string): string;
Returns part of string s between substrings s1 and s2.

function StrGet(var S: string; I: Integer): Char;

procedure StrSet(c: Char; I: Integer; var s: string);

function Uppercase(s: string): string;
Returns the specified string in upper case.

function Lowercase(s: string): string;
Returns the specified string in lowercase.

function Trim(s: string): string;
Removes spaces from start and end of string.

function Length(s: string): LongInt;
Returns string length in characters.

function Left(Text: string; Count: Integer): string;
Returns a part of the leftside of a string, the length is specified by Count.

function Right(Text: string; Count: Integer): string;
Returns a part of the rightside of a string, the length is specified by Count.

function Replace(Text, FindStr, ReplaceStr: string): string;
Replaces all instances of FindStr in Text by ReplaceStr.

function GetNumbers(Text: string): string;
Returns all numerical chars found in a string.

function GetLetters(Text: string): string;
Returns all alphabetical chars found in a string.

function GetOthers(Text: string): string;
Returns all non-numerical and non-alphabetical chars in a string.

function TrimNumbers(Text: string): string;
Removes all numerical chars from a string.

function TrimLetters(Text: string): string;
Removes all alphabetical chars from a string.

function TrimOthers(Text: string): string;
Removes all chars that are not numerical and not alphabetical from a string.

function Capitalize(S: string): string;
Will capitalize strings.
Example:
hello world => Hello World

function PosEx(search, s: string; from: Integer): Integer;
Returns the position of the substring in s searching forwards from from.

function LastPosEx(search, s: string; from: Integer): Integer;
Returns the position of the substring in s searching backwards from from.

function LastPos(search, s: string): Integer;
Returns the position of the substring in s searching backwards from the end of the string.

function StartsWith(prefix, s: string): Boolean;
Returns true if s starts with prefix.

function EndsWith(suffix, s: string): Boolean;
Returns true if s ends with suffix.

function TrimEx(delimiter, s: string): string;
Trims the specified delimiter from the start and end of the string.

function RegexPos(Txt, Regex: string): Integer;
Returns the position of the first found occurence of the regex.

function FindRegex(Txt, Regex: string): string;
Returns the string found by the regex.

function ReplaceRegex(Txt, Regex, ReplaceStr: string): string;
Replaces all occurences of the found regex by ReplaceStr.

function Format(s: string; data: array of const): string;
Formats a string with data.

function Implode(Glue: string; Pieces: TStringArray): string;
Implodes strings into 1 string.

function Explode(Separator, s: string): TStringArray;
Explodes a string into pieces.

function ExplodeEx(Separator, s: string; Limit: Integer): TStringArray;
Explodes a string into pieces with a Limit.

~shut


^ Any For Simba???

Gu3st...

2011-02-18 05:23:02

From: anonymity
^ Any For Simba???
Most of these are in Simba already

anonymity

2011-02-18 11:00:14

Hmm.. alright. I was trying to use

function Length(s: string): LongInt;
Returns string length in characters.

function Left(Text: string; Count: Integer): string;
Returns a part of the leftside of a string, the length is specified by Count.

function Right(Text: string; Count: Integer): string;
Returns a part of the rightside of a string, the length is specified by Count.

function Replace(Text, FindStr, ReplaceStr: string): string;
Replaces all instances of FindStr in Text by ReplaceStr.
and was having some troubles. I wasn't sure if they were included in simba

**BANNED The Man

2011-02-18 11:29:53

Length and Replace are 100% in simba, the other 2 i have not tried.

-Boom

anonymity

2011-02-18 11:34:52

Yea, unfortunately.. the other two are the ones more required..