V.I.P FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.
HELLO, Visitor

Handling and Conversions

Go down

Handling and Conversions Empty Handling and Conversions

Post by winda Sat Oct 15, 2011 12:01 am

String Handling

You can access a specific character in a string if you put the number of the position of that character in square brackets behind a string.
program Strings;

var
s: String;
c: Char;

begin
s := 'Hello';
c := s[1];{c = 'H'}
end.
You can get the length of a string using the Length command.
program Strings;

var
s: String;
l: Integer;

begin
s := 'Hello';
l := Length(s);{l = 5}
end.
To find the position of a string within a string use the Pos command.
Parameters:
1: String to find
2: String to look in
program Strings;

var
s: String;
p: Integer;

begin
s := 'Hello world';
p := Pos('world',s);
end.
The Delete command removes characters from a string.
Parameters:
1: String to delete characters from
2: Position to start deleting from
3: Amount of characters to delete
program Strings;

var
s: String;

begin
s := 'Hello';
Delete(s,1,1);{s = 'ello'}
end.
The Copy command is like the square brackets but can access more than just one character.
Parameters:
1: String to copy characters from
2: Position to copy from
3: Amount of characters to copy
program Strings;

var
s, t: String;

begin
s := 'Hello';
t := Copy(s,1,3);{t = 'Hel'}
end.
Insert will insert characters into a string at a certain position.
Parameters:
1: String that will be inserted into the other string
2: String that will have characters inserted into it
3: Position to insert characters
program Strings;

var
s: String;

begin
s := 'Hlo';
Insert('el',s,2);
end.
The ParamStr command will give you the command-line parameters that were passed to a program. ParamCount will tell you how many parameters were passed to the program. Parameter 0 is always the program's name and from 1 upwards are the parameters that have been typed by the user.
program Strings;

var
s: String;
i: Integer;

begin
s := ParamStr(0);
i := ParamCount;
end.
Conversions

The Str command converts an integer to a string.
program Convert;

var
s: String;
i: Integer;

begin
i := 123;
Str(i,s);
end.
The Val command converts a string to an integer.
program Convert;

var
s: String;
i: Integer;
e: Integer;

begin
s := '123';
Val(s,i,e);
end.
Int will give you the number before the comma in a real number.
program Convert;

var
r: Real;

begin
r := Int(3.14);
end.
Frac will give you the number after the comma in a real number.
program Convert;

var
r: Real;

begin
r := Frac(3.14);
end.
Round will round off a real number to the nearest integer.
program Convert;

var
i: Integer;

begin
i := Round(3.14);
end.
Trunc will give you the number before the comma of a real number as an integer.
program Convert;

var
i: Integer;

begin
i := Trunc(3.14);
end.
Computers use the numbers 0 to 255(1 byte) to represent characters internally and these are called ASCII characters. The Ord command will convert a character to number and the Chr command will convert a number to a character. Using a # in front of a number will also convert it to a character.
program Convert;

var
b: Byte;
c: Char;

begin
c := 'a';
b := Ord(c);
c := Chr(b);
c := #123;
end.
The UpCase command changes a character from a lowercase letter to and uppercase letter.
program Convert;

var
c: Char;

begin
c := 'a';
c := UpCase(c);
end.
There is no lowercase command but you can do it by adding 32 to the ordinal value of an uppercase letter and then changing it back to a character.
Extras

The Random command will give you a random number from 0 to the number you give it - 1. The Random command generates the same random numbers every time you run a program so the Randomize command is used to make them more random by using the system clock.
program Rand;

var
i: Integer;

begin
Randomize;
i := Random(101);
end.
winda
winda
Calon Penduduk Cyber
Calon Penduduk Cyber

Jumlah posting : 19
Points : 4624
Reputation : 4
Join date : 14.10.11

http://www.cyber.forum-canada.com

Kembali Ke Atas Go down

Kembali Ke Atas


 
Permissions in this forum:
Anda tidak dapat menjawab topik