I blogged quite some time ago about record helpers for native types in XE3 in general. This time I want to focus on TStringHelper. This is a new intrinsic type (or native type) helper class, that is a new way to add "methods" to a native type. In other words, rather than writing:

Length (string1);

you can now write

string1.Length;

Not a big deal, you might think. If so, think twice. In the former case, if you don't remember the function name, you get little or no help from Code Insight (since wading through the huge list of global functions is almost pointless). With the latter coding style, you just type string1, add the dot, wait, and you get a list of "operations" you can perform on the string. You can also do methods chaining, like

string1.Trim.Length;

which is equivalent to:

Length(Trim(string1));

Now, Length or Trim is quite easy to remember, but not all string related functions are so trivial. And if you consider a new developer approaching Delphi (or getting back after a few years), the Code Insight mechanism is really going to help.

There is a second consideration for TStringHelper. In the methods of this helper class, all string indexes are 0-based. Like it happens in most other programming languages, but unlike traditional Delphi code. Even traditional Delphi uses 0-based indexes for dynamic arrays, containers and collections, and any sort of internal lists, but the strings. I'm saying traditional because, while standard string functions are indeed 1-based, the interpretation of:

string1[nIndex]

in Delphi XE3 can be modified using a new compiler setting, ZEROBASEDSTRINGS. This is off by default, but you can turn it on. Notice that this setting doesn't change the string structure in any way. There is no such a thing as a zero-based string and a one-based string. A string is just a string. There are string access operators and functions that treat the indexes and elements positions with a numbering starting at 0 or 1. That's it. Even if this would be very confusing, you can mix different access mechanisms in a single unit, or within different units in a single project.

Finally, let me list some of the methods of the TStringHelper class (many of which have multiple overloaded versions):

Compare
CompareTo
Contains
Copy
CopyTo
EndsText
EndsWith
Format
IndexOf
IndexOfAny
Insert
IsDelimiter
IsEmpty
IsNullOrEmpty
IsNullOrWhiteSpace
Join
LastDelimiter
LastIndexOf
LastIndexOfAny
PadLeft
PadRight
Remove
Replace
Split
StartsWith
Substring
StartIndex
ToCharArray
ToLower
ToLowerInvariant
ToUpper
ToUpperInvariant
Trim
TrimEnd
TrimStart

As I'll detail soon, the push for TStringHelper and most of the related changes are also tied to Delphi shift to mobile. Stay tuned for more information.