0

I need a StringHelper which saves a string to a file:

var
  s: string;
begin
  s := 'Some text';
  s.SaveTo('C:\MyText.txt');
end;

Unfortunately, this is not possible. Is it possible to add such a StringHelper?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 2
    Err, why do you say "this is not possible" and then immediately ask whether it is? – MartynA Dec 07 '15 at 17:13
  • 1
    @MartynA It is clear that this is a semantic misunderstanding, and my question should be understood. So why do you ask? – user1580348 Dec 07 '15 at 17:22
  • 1
    @user1580348 Because we care about quality. We want to see well written and clear questions. Please take the time to clarify the text. – David Heffernan Dec 07 '15 at 17:59

1 Answers1

6

It is possible to add such a helper. For instance:

type
  TMyStringHelper = record helper for string
    procedure SaveTo(const FileName: string);
  end;

The downside to doing so is that this will replace the string helper that is provided by the RTL. If you don't use it, that won't matter. If you do use it, then that's a problem that cannot readily be overcome.

You could look at this a different way. Instead of trying to use a helper on the string type, you could use TFile.WriteAllText instead.

TFile.WriteAllText(FileName, 'Some text', TEncoding.UTF8);

Obviously you can use a different encoding if you prefer.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490