About String Types

A string represents a sequence of characters. Delphi supports the following predefined string types. String types

Type Maximum length Memory required Used for

ShortString255 characters 2 to 256 bytes backward compatibility

AnsiString ~2A31 characters 4 bytes to 2GB 8-bit (ANSI) characters, DBCS ANSI, MBCS ANSI, etc.

WideString~2A30 characters 4 bytes to 2GB Unicode characters; multi-user servers and multi-language applications

On the Win32 platform, AnsiString, sometimes called the long string, is the preferred type for most purposes. WideString is the preferred string type on the .NET platform.

String types can be mixed in assignments and expressions; the compiler automatically performs required conversions. But strings passed by reference to a function or procedure (as var and out parameters) must be of the appropriate type. Strings can be explicitly cast to a different string type.

The reserved word string functions like a generic type identifier. For example, var s: string;

creates a variable s that holds a string. On the Win32 platform, the compiler interprets string (when it appears without a bracketed number after it) as AnsiString. On the .NET platform, the string type maps to the String class. You can use single byte character strings on the .NET platform, but you must explicitly declare them to be of type AnsiString.

On the Win32 platform, you can use the {$h-} directive to turn string into ShortString. The {$h-} directive is deprecated on the .NET platform.

The standard function Length returns the number of characters in a string. The setLength procedure adjusts the length of a string.

Comparison of strings is defined by the ordering of the characters in corresponding positions. Between strings of unequal length, each character in the longer string without a corresponding character in the shorter string takes on a greater-than value. For example, 'AB' is greater than 'A'; that is, 'AB' > 'A' returns True. Zero-length strings hold the lowest values.

You can index a string variable just as you would an array. If s is a string variable and i an integer expression, S[i] represents the ith character - or, strictly speaking, the ith byte in s. For a ShortString or AnsiString, S[i] is of type AnsiChar; for a WideString, S[i] is of type WideChar. For single-byte (Western) locales, MyString[2] := 'A'; assigns the value a to the second character of MyString. The following code uses the standard AnsiUpperCase function to convert MyString to uppercase.

var I: Integer; begin

MyString[I] := AnsiUpperCase(MyString[I]); I := I - 1; end;


Be careful indexing strings in this way, since overwriting the end of a string can cause access violations. Also, avoid passing long-string indexes as var parameters, because this results in inefficient code.

You can assign the value of a string constant - or any other expression that returns a string - to a variable. The length of the string changes dynamically when the assignment is made. Examples:

MyString :=

MyString :=

MyString :=

MyString :=

MyString :=

'Hello world!'; 'Hello' + 'world'; MyString + '!'; ' '; { space } ''; { empty string }

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment