是否保存到StringHelper? [英] SaveTo StringHelper?

查看:53
本文介绍了是否保存到StringHelper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个将字符串保存到文件的StringHelper:

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

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

不幸的是,这是不可能的.是否可以添加这样的StringHelper?

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

推荐答案

可以添加这样的帮助器.例如:

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

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

这样做的缺点是它将替换RTL提供的字符串帮助程序.如果您不使用它,那将无关紧要.如果您确实使用它,那么这是一个无法轻易克服的问题.

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.

您可以用另一种方式看待.可以尝试使用 string 类型上使用帮助程序.="noreferrer"> TFile.WriteAllText .

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.

这篇关于是否保存到StringHelper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆