STRING / PCHAR / CHAR in Delphi
STRING / PCHAR / CHAR in Delphi
---------------------------------------------
Char
-------
Char is a data type that holds only one character as value. Ansi Characters are used to store single byte ansi characters. Unicode
characters are used to store 2byte characters that is to cupport foreign languages like chinese, japanese etc.
var
chr1 : Char; // Holds a single character, alphabet
chr2 : WideChar; // Holds a single character, International alphabet // for Unicode support
chr3 : AnsiChar; // Holds a single character, alphabet
NON-PRINTING OR CONTROL CHARACTERS, NOT USED IN THIS CLASS, INCLUDED FOR INTEREST ONLY
0 Null (nothing)
7 Bell
8 Back space
9 Tab
10 Line feed (new line at present column)
13 Carriage return (return to beginning of line)
26 End of file
27 [Esc] (Escape key)
ASCII CHARACTER CODES
32 [space] 64 @ 96 `
33 ! 65 A 97 a
34 " 66 B 98 b
35 # 67 C 99 c
36 $ 68 D 100 d
37 % 69 E 101 e
38 & 70 F 102 f
39 ' 71 G 103 g
40 ( 72 H 104 h
41 ) 73 I 105 i
42 * 74 J 106 j
43 + 75 K 107 k
44 , 76 L 108 l
45 - 77 M 109 m
46 . 78 N 110 n
47 / 79 O 111 o
48 0 80 P 112 p
49 1 81 Q 113 q
50 2 82 R 114 r
51 3 83 S 115 s
52 4 84 T 116 t
53 5 85 U 117 u
54 6 86 V 118 v
55 7 87 W 119 w
56 8 88 X 120 x
57 9 89 Y 121 y
58 : 90 Z 122 z
59 ; 91 [ 123 {
60 < 92 \ 124 |
61 = 93 ] 125 }
62 > 94 ^ 126 ~
63 ? 95 _
String
--------
String is a data type this kind of variables holds sequence of characters as value. In old version String = Ansistring.
But in new versions from Delphi 2010 it is Unicodestring / WideString. String starts with index 1 and 0 index stores
length of the string. SO we can find end of string by its length. Maximum length is depend on available memory. In Delphi string
is reference counted. So delphi will automatically frees a string datatype. Delphi doesn't support automatic type conversions
betwwen Ansi and Wide string types.
var
Str4 : ShortString; // Holds a string of up to 255 Char's
Str5 : String; // Holds strings of Char's of any size desired
Str6 : AnsiString; // Holds strings of AnsiChar's any size desired
Str7 : WideString; // Holds strings of WideChar's of any size desired // for Unicode support/ so same as Unicodestring
str9 : Unicodestring// same as widestring
str8 : string[20]; // maximum 20 characters
String system functions
AnsiLeftStr Returns leftmost characters of a string
AnsiMidStr Returns middle characters of a string
AnsiRightStr Returns rightmost characters of a string
AnsiStartsStr Does a string start with a substring?
AnsiContainsStr Does a string contain another?
AnsiEndsStr Does a string end with a substring?
AnsiIndexStr Check substring list against a string
AnsiMatchStr Check substring list against a string
AnsiReverseString Reverses characters in a string
AnsiReplacStr Replaces all substring occurences
DupeString Repeats a substring n times
StrScan Scans a string for a specific character
StuffString Replaces part of a string text
Trim Removes leading and trailing white space
TrimLeft Removes leading white space
TrimRight Removes trailing white space
Converting from numbers to strings
CurrToStrF Convert a currency value to a string with formatting
DateTimeToStr Converts TDateTime date and time values to a string
DateTimeToString Rich formatting of a TDateTime variable into a string
DateToStr Converts a TDateTime date value to a string
FloatToStr Convert a floating point value to a string
FloatToStrF Convert a floating point value to a string with formatting
Format Rich formatting of numbers and text into a string
FormatCurr Rich formatting of a currency value into a string
FormatDateTime Rich formatting of a TDateTime variable into a string
FormatFloat Rich formatting of a floating point number into a string
IntToHex Convert an Integer into a hexadecimal string
IntToStr Convert an integer into a string
Str Converts an integer or floating point number to a string
Converting from strings to numbers
StringToWideChar Converts a string into a WideChar 0 terminated buffer
StrToCurr Convert a number string into a currency value
StrToDate Converts a date string into a TDateTime value
StrToDateTime Converts a date+time string into a TDateTime value
StrToFloat Convert a number string into a floating point value
StrToInt Convert an integer string into an Integer value
StrToInt64 Convert an integer string into an Int64 value
StrToInt64Def Convert a string into an Int64 value with default
StrToIntDef Convert a string into an Integer value with default
StrToTime Converts a time string into a TDateTime value
Val Converts number strings to integer and floating point values
PCHAR
----------
Pchar is a datatype (pointer of characters) that holds array of characters or single character. It is null terminated string like C.
It starts from 0 index. In Delphi it is mostly used for Windows API function calls. Since the array has no length indicator, Delphi uses
the ASCII 0 (NULL; #0) character to mark the boundary of the string. It allowed strings of any length, limited only by how much memory
the native pointer type can access.
var
p1: Pchar;
chr1: char;
s1: string;
chr2: char;
.
.
p1:='abcdefgh';
chr1:=p1[0];
s1:=p1;
chr2:=s1[1];
-------------------------------------------------------------------------
procedure TForm1.Button1Click (Sender: TObject);
var
S1: String;
begin
SetLength (S1, 100);
GetWindowText (Handle, PChar (S1), Length (S1));
Button1.Caption := S1;
end;
S1 := String (PChar (S1));
---------------------------------------------
Char
-------
Char is a data type that holds only one character as value. Ansi Characters are used to store single byte ansi characters. Unicode
characters are used to store 2byte characters that is to cupport foreign languages like chinese, japanese etc.
var
chr1 : Char; // Holds a single character, alphabet
chr2 : WideChar; // Holds a single character, International alphabet // for Unicode support
chr3 : AnsiChar; // Holds a single character, alphabet
NON-PRINTING OR CONTROL CHARACTERS, NOT USED IN THIS CLASS, INCLUDED FOR INTEREST ONLY
0 Null (nothing)
7 Bell
8 Back space
9 Tab
10 Line feed (new line at present column)
13 Carriage return (return to beginning of line)
26 End of file
27 [Esc] (Escape key)
ASCII CHARACTER CODES
32 [space] 64 @ 96 `
33 ! 65 A 97 a
34 " 66 B 98 b
35 # 67 C 99 c
36 $ 68 D 100 d
37 % 69 E 101 e
38 & 70 F 102 f
39 ' 71 G 103 g
40 ( 72 H 104 h
41 ) 73 I 105 i
42 * 74 J 106 j
43 + 75 K 107 k
44 , 76 L 108 l
45 - 77 M 109 m
46 . 78 N 110 n
47 / 79 O 111 o
48 0 80 P 112 p
49 1 81 Q 113 q
50 2 82 R 114 r
51 3 83 S 115 s
52 4 84 T 116 t
53 5 85 U 117 u
54 6 86 V 118 v
55 7 87 W 119 w
56 8 88 X 120 x
57 9 89 Y 121 y
58 : 90 Z 122 z
59 ; 91 [ 123 {
60 < 92 \ 124 |
61 = 93 ] 125 }
62 > 94 ^ 126 ~
63 ? 95 _
String
--------
String is a data type this kind of variables holds sequence of characters as value. In old version String = Ansistring.
But in new versions from Delphi 2010 it is Unicodestring / WideString. String starts with index 1 and 0 index stores
length of the string. SO we can find end of string by its length. Maximum length is depend on available memory. In Delphi string
is reference counted. So delphi will automatically frees a string datatype. Delphi doesn't support automatic type conversions
betwwen Ansi and Wide string types.
var
Str4 : ShortString; // Holds a string of up to 255 Char's
Str5 : String; // Holds strings of Char's of any size desired
Str6 : AnsiString; // Holds strings of AnsiChar's any size desired
Str7 : WideString; // Holds strings of WideChar's of any size desired // for Unicode support/ so same as Unicodestring
str9 : Unicodestring// same as widestring
str8 : string[20]; // maximum 20 characters
String system functions
AnsiLeftStr Returns leftmost characters of a string
AnsiMidStr Returns middle characters of a string
AnsiRightStr Returns rightmost characters of a string
AnsiStartsStr Does a string start with a substring?
AnsiContainsStr Does a string contain another?
AnsiEndsStr Does a string end with a substring?
AnsiIndexStr Check substring list against a string
AnsiMatchStr Check substring list against a string
AnsiReverseString Reverses characters in a string
AnsiReplacStr Replaces all substring occurences
DupeString Repeats a substring n times
StrScan Scans a string for a specific character
StuffString Replaces part of a string text
Trim Removes leading and trailing white space
TrimLeft Removes leading white space
TrimRight Removes trailing white space
Converting from numbers to strings
CurrToStrF Convert a currency value to a string with formatting
DateTimeToStr Converts TDateTime date and time values to a string
DateTimeToString Rich formatting of a TDateTime variable into a string
DateToStr Converts a TDateTime date value to a string
FloatToStr Convert a floating point value to a string
FloatToStrF Convert a floating point value to a string with formatting
Format Rich formatting of numbers and text into a string
FormatCurr Rich formatting of a currency value into a string
FormatDateTime Rich formatting of a TDateTime variable into a string
FormatFloat Rich formatting of a floating point number into a string
IntToHex Convert an Integer into a hexadecimal string
IntToStr Convert an integer into a string
Str Converts an integer or floating point number to a string
Converting from strings to numbers
StringToWideChar Converts a string into a WideChar 0 terminated buffer
StrToCurr Convert a number string into a currency value
StrToDate Converts a date string into a TDateTime value
StrToDateTime Converts a date+time string into a TDateTime value
StrToFloat Convert a number string into a floating point value
StrToInt Convert an integer string into an Integer value
StrToInt64 Convert an integer string into an Int64 value
StrToInt64Def Convert a string into an Int64 value with default
StrToIntDef Convert a string into an Integer value with default
StrToTime Converts a time string into a TDateTime value
Val Converts number strings to integer and floating point values
PCHAR
----------
Pchar is a datatype (pointer of characters) that holds array of characters or single character. It is null terminated string like C.
It starts from 0 index. In Delphi it is mostly used for Windows API function calls. Since the array has no length indicator, Delphi uses
the ASCII 0 (NULL; #0) character to mark the boundary of the string. It allowed strings of any length, limited only by how much memory
the native pointer type can access.
var
p1: Pchar;
chr1: char;
s1: string;
chr2: char;
.
.
p1:='abcdefgh';
chr1:=p1[0];
s1:=p1;
chr2:=s1[1];
-------------------------------------------------------------------------
procedure TForm1.Button1Click (Sender: TObject);
var
S1: String;
begin
SetLength (S1, 100);
GetWindowText (Handle, PChar (S1), Length (S1));
Button1.Caption := S1;
end;
S1 := String (PChar (S1));
Comments
Post a Comment