|
|
Text types |
|
Like many other languages, Delphi allows you to store letters, words, and sentences in single variables. These can be used to store and display such things as user details, screen titles and so on. A letter is stored in a single character variable type, such as Char, and words and sentences stored in string types, such as String. Here are the different text types in Delphi:
|
|
|
var
Str1 : Char; // Holds a single character, small alphabet
Str2 : WideChar; // Holds a single character, International alphabet
Str3 : AnsiChar; // Holds a single character, small alphabet
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
|
|
|
We'll cover the character and string types in turn, and then look at some of the large range of string processing routines provided by the Delphi run time library.
|
|
|
Characters |
|
Single character variables hold a single character of text. Normally, this can be held in one byte. AnsiChar types are exactly one byte in size, and can hold any of the characters in the Ansi character set.
|
|
|
The Ansi character set |
|
Char Code Description
9 Tab
10 Line feed
13 Carriage return
' ' 32 Space
! 33 Exclamation mark
" 34 Quotation mark
# 35 Number sign
$ 36 Dollar sign
% 37 Percent sign
& 38 Ampersand
' 39 Apostrophe
( 40 Left parenthesis
) 41 Right parenthesis
* 42 Asterisk
+ 43 Plus sign
, 44 Comma
- 45 Hyphen-minus
. 46 Full stop
/ 47 Solidus
0 48 Digit zero
1 49 Digit one
2 50 Digit two
3 51 Digit three
4 52 Digit four
5 53 Digit five
6 54 Digit six
7 55 Digit seven
8 56 Digit eight
9 57 Digit nine
: 58 Colon
; 59 Semicolon
< 60 Less-than sign
= 61 Equals sign
> 62 Greater-than sign
? 63 Question mark
@ 64 Commercial at
A 65 Latin capital letter A
B 66 Latin capital letter B
C 67 Latin capital letter C
D 68 Latin capital letter D
E 69 Latin capital letter E
F 70 Latin capital letter F
G 71 Latin capital letter G
H 72 Latin capital letter H
I 73 Latin capital letter I
J 74 Latin capital letter J
K 75 Latin capital letter K
L 76 Latin capital letter L
M 77 Latin capital letter M
N 78 Latin capital letter N
O 79 Latin capital letter O
P 80 Latin capital letter P
Q 81 Latin capital letter Q
R 82 Latin capital letter R
S 83 Latin capital letter S
T 84 Latin capital letter T
U 85 Latin capital letter U
V 86 Latin capital letter V
W 87 Latin capital letter W
X 88 Latin capital letter X
Y 89 Latin capital letter Y
Z 90 Latin capital letter Z
[ 91 Left square bracket
\ 92 Reverse solidus
] 93 Right square bracket
^ 94 Circumflex accent
_ 95 Low line
` 96 Grave accent
a 97 Latin small letter a
b 98 Latin small letter b
c 99 Latin small letter c
d 100 Latin small letter d
e 101 Latin small letter e
f 102 Latin small letter f
g 103 Latin small letter g
h 104 Latin small letter h
i 105 Latin small letter i
j 106 Latin small letter j
k 107 Latin small letter k
l 108 Latin small letter l
m 109 Latin small letter m
n 110 Latin small letter n
o 111 Latin small letter o
p 112 Latin small letter p
q 113 Latin small letter q
r 114 Latin small letter r
s 115 Latin small letter s
t 116 Latin small letter t
u 117 Latin small letter u
v 118 Latin small letter v
w 119 Latin small letter w
x 120 Latin small letter x
y 121 Latin small letter y
z 122 Latin small letter z
{ 123 left curly bracket
| 124 Vertical line
} 125 Right curly bracket
~ 126 Tilde
127 (not used)
? 128 Euro sign Currency Symbols
? 129 (not used)
? 130 Single low-9 quotation mark General Punctuation
? 131 Latin small letter f with hook Latin Extended-B
? 132 Double low-9 quotation mark General Punctuation
? 133 Horizontal ellipsis General Punctuation
? 134 Dagger General Punctuation
? 135 Double dagger General Punctuation
? 136 Modifier letter circumflex accent Spacing Modifier Letters
? 137 Per mille sign General Punctuation
? 138 Latin capital letter S with caron Latin Extended-A
? 139 Single left-pointing angle quotation mark General Punctuation
? 140 Latin capital ligature OE Latin Extended-A
? 141 (not used)
? 142 Latin capital letter Z with caron Latin Extended-A
? 143 (not used)
? 144 (not used)
? 145 Left single quotation mark General Punctuation
? 146 Right single quotation mark General Punctuation
? 147 Left double quotation mark General Punctuation
? 148 Right double quotation mark General Punctuation
? 149 Bullet General Punctuation
? 150 En dash General Punctuation
? 151 Em dash General Punctuation
? 152 Small tilde Spacing Modifier Letters
? 153 Trade mark sign Letterlike Symbols
? 154 Latin small letter s with caron Latin Extended-A
? 155 Single right-pointing angle quotation mark General Punctuation
? 156 Latin small ligature oe Latin Extended-A
? 157 (not used)
? 158 Latin small letter z with caron Latin Extended-A
? 159 Latin capital letter Y with diaeresis Latin Extended-A
160 No-break space
? 161 Inverted exclamation mark
? 162 Cent sign
? 163 Pound sign
? 164 Currency sign
? 165 Yen sign
? 166 Broken bar
? 167 Section sign
? 168 Diaeresis
? 169 Copyright sign
? 170 Feminine ordinal indicator
? 171 Left-pointing double angle quotation mark
? 172 Not sign
? 173 Soft hyphen
? 174 Registered sign
? 175 Macron
? 176 Degree sign
? 177 Plus-minus sign
? 178 Superscript two
? 179 Superscript three
? 180 Acute accent
? 181 Micro sign
? 182 Pilcrow sign
? 183 Middle dot
? 184 Cedilla
? 185 Superscript one
? 186 Masculine ordinal indicator
? 187 Right-pointing double angle quotation mark
? 188 Vulgar fraction one quarter
? 189 Vulgar fraction one half
? 190 Vulgar fraction three quarters
? 191 Inverted question mark
? 192 Latin capital letter A with grave
? 193 Latin capital letter A with acute
? 194 Latin capital letter A with circumflex
? 195 Latin capital letter A with tilde
? 196 Latin capital letter A with diaeresis
? 197 Latin capital letter A with ring above
? 198 Latin capital letter AE
? 199 Latin capital letter C with cedilla
? 200 Latin capital letter E with grave
? 201 Latin capital letter E with acute
? 202 Latin capital letter E with circumflex
? 203 Latin capital letter E with diaeresis
? 204 Latin capital letter I with grave
? 205 Latin capital letter I with acute
? 206 Latin capital letter I with circumflex
? 207 Latin capital letter I with diaeresis
? 208 Latin capital letter Eth
? 209 Latin capital letter N with tilde
? 210 Latin capital letter O with grave
? 211 Latin capital letter O with acute
? 212 Latin capital letter O with circumflex
? 213 Latin capital letter O with tilde
? 214 Latin capital letter O with diaeresis
? 215 Multiplication sign
? 216 Latin capital letter O with stroke
? 217 Latin capital letter U with grave
? 218 Latin capital letter U with acute
? 219 Latin capital letter U with circumflex
? 220 Latin capital letter U with diaeresis
? 221 Latin capital letter Y with acute
? 222 Latin capital letter Thorn
? 223 Latin small letter sharp s
? 224 Latin small letter a with grave
? 225 Latin small letter a with acute
? 226 Latin small letter a with circumflex
? 227 Latin small letter a with tilde
? 228 Latin small letter a with diaeresis
? 229 Latin small letter a with ring above
? 230 Latin small letter ae
? 231 Latin small letter c with cedilla
? 232 Latin small letter e with grave
? 233 Latin small letter e with acute
? 234 Latin small letter e with circumflex
? 235 Latin small letter e with diaeresis
? 236 Latin small letter i with grave
? 237 Latin small letter i with acute
? 238 Latin small letter i with circumflex
? 239 Latin small letter i with diaeresis
? 240 Latin small letter eth
? 241 Latin small letter n with tilde
? 242 Latin small letter o with grave
? 243 Latin small letter o with acute
? 244 Latin small letter o with circumflex
? 245 Latin small letter o with tilde
? 246 Latin small letter o with diaeresis
? 247 Division sign
? 248 Latin small letter o with stroke
? 249 Latin small letter u with grave
? 250 Latin small letter u with acute
? 251 Latin small letter with circumflex
? 252 Latin small letter u with diaeresis
? 253 Latin small letter y with acute
? 254 Latin small letter thorn
? 255 Latin small letter y with diaeresis
|
|
|
Notice that the digits come before the upper case letters which come before the lower case letters.
|
|
|
Assigning to and from character variables |
|
Here are some examples of characters, along with assignments to and from them:
|
|
|
var
lower, upper, copied, fromNum : AnsiChar;
begin
lower := 'a'; // Assign a lower case letter
upper := 'Q'; // Assign an upper case letter
copied := lower; // Assign from another character variable
fromNum := Chr(65); // Assign using a function
end;
|
|
|
These character variables are now set to these values:
lower : 'a'
upper : 'Q'
copied : 'a'
fromNum : 'A'
|
|
|
Notice the use of a run time library function Chr to convert a number to a character. We can use the Ord function to convert a character into a number:
|
|
|
var
myNum : Byte;
begin
myNum := Ord('A'); // myNum is set to 65
end;
|
|
|
What are WideChar types? |
|
The ansi character set derived from the earlier ascii character set. Both were designed around European characters, which comfortably fitted into 256 values, the capacity of a single byte. For a long time, this was the easy way to handle text. But this left many countries, especially in Asia, out of the picture.
|
|
|
The WideChar type can support double-byte characters, which can hold numeric representations of the vast alphabets of China, Japan and so on. These are called International characters. International applications must use WideChar and WideString types.
|
|
|
Strings |
|
A single character is useful when parsing text, one character at a time. However, to handle words and sentences and screen labels and so on, strings are used. A string is literally a string of characters. It can be a string of Char, AnsiChar or WideChar characters.
|
|
|
Assigning to and from a string |
|
A ShortString is a fixed 255 characters long. A String (by default) is the same as an AnsiString, and is of any length you want. WideStrings can also be of any length. Their storage is dynamically handled. In fact, if you copy one string to another, the second will just point to the contents of the first.
|
|
|
Here are some assignments:
|
|
|
var
source, target, last : String;
begin
source := 'Hello World'; // Assign from a string literal
target := source; // Assign from another variable
last := 'Don''t do that'; // Quotes in a string must be doubled
end;
|
|
|
source is now set to : Hello World
target is now set to : Hello World
last is now set to : Don't do that
|
|
|
String operators |
|
There are a number of primitive string operators that are commonly used:
|
|
|
+ Concatenates two strings together
= Compares for string equality
< Is one string lower in sequence than another
<= Is one string lower or equal in sequence with another
> Is one string greater in sequence than another
>= Is one string greater or equal in sequence with another
<> Compares for string inequality
|
|
|
Here are some examples using these operators:
|
|
|
var
myString : string;
begin
myString := 'Hello ' + 'World'; // String concatenation
if 'ABC' = 'abc' // Equality
then ShowMessage('ABC = abc');
if 'ABC' = 'ABC' // Equality
then ShowMessage('ABC = ABC');
if 'ABC' < 'abc' // Less than
then ShowMessage('ABC < abc');
if 'ABC' <= 'abc' // Less than or equal
then ShowMessage('ABC <= abc');
if 'ABC' > 'abc' // Greater than
then ShowMessage('ABC > abc');
if 'ABC' >= 'abc' // Greater than or equal
then ShowMessage('ABC >= abc');
if 'ABC' <> 'abc' // Inequality
then ShowMessage('ABC <> abc');
end;
|
|
|
ABC = ABC
ABC < abc
ABC <= abc
ABC <> abc
|
|
|
String processing routines |
|
There are a number of string manipulation routines that are given by example below. Click on any of them to learn more (and also click on WrapText for another, more involved routine).
|
|
|
var
Source, Target : string;
begin
Source := '12345678';
Target := Copy(Source, 3, 4); // Target now = '3456'
Target := '12345678';
Insert('-+-', Target, 3); // Target now = '12-+-345678'
Target := '12345678';
Delete(Target, 3, 4); // Target now = '1278'
Target := StringOfChar('S', 5); // Target now = 'SSSSS'
Source := 'This is a way to live A big life';
// Target set to 'This is THE way to live THE big life'
Target := StringReplace(before, ' a ', ' THE ',
[rfReplaceAll, rfIgnoreCase]);
end;
|
|
|
Click on the following to learn more:
|
|
|
|
|
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
|
|
|
|
|