System.SysUtils.TStringHelper

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

TStringHelper = record helper for string

C++

struct TStringHelper /* Helper for uni  string 'System::UnicodeString' */;

Properties

Type Visibility Source Unit Parent
helper
class
public
System.SysUtils.pas
System.SysUtils.hpp
System.SysUtils System.SysUtils

Description

Record helper that provides functions and properties for working with all strings, including both 1-based and 0-based strings.

TStringHelper is a record helper for the intrinsic string type and provides helper methods that are accessible from string types as if the helper methods were methods of a class or record.

The Delphi mobile compilers (DCCIOS32 and DCCIOSARM) introduce 0-based indexing for strings. The Delphi desktop compilers continue to support 1-based strings, but interfaces and dynamic arrays are 0-based for all Delphi compilers. This change is further discussed in Migrating Delphi Code to Mobile from Desktop.

Indexing for string types is different according to the compiler in use, as shown in the following table.

Indexing of Strings in the Delphi Compilers
Delphi Compilers String Indexing

Delphi mobile compilers

  • DCCIOSARM
  • DCCIOS32

0-based

(the starting index of the first character in a string is zero)

Delphi desktop compilers

  • DCC32
  • DCC64
  • DCCOSX

1-based

(the starting index of the first character in the string is one)

Always using TStringHelper methods to handle strings enables you to write code for multiple compilers and multiple platforms without using conditionals such as the IFDEF directive.

Example

If your code previously used the System.Pos function with a string called S, you can now edit the code to use the TStringHelper.IndexOf method. You can call S.IndexOf() to perform the same function:

var
  MyString: String;

begin
  MyString := 'This is a string.';

  Writeln(myString.IndexOf('a', 0, 0)); //Working with zero-based or 1-based strings
  Writeln(Pos('a', myString)); //Working with 1-based strings
end.

Output:

8
9
Note: You are encouraged to move to the TSringHelper methods when manipulating strings on all platforms. To preserve backward compatibility with existing code as much as possible, the longstanding functions in the System unit that deal with string indices will continue to function with 1-based strings, and also perform the necessary automatic conversions to do the correct operations on 0-based strings as well. One example is System.Pos. However, these older System unit string-handling functions are deprecated and support might be removed in the future.

See Also