1
我有一个抽象类Writer
,它允许客户写入。可能是屏幕,可能是一个文件。现在,我尝试创建派生类来写入字符串。在Matlab中正确写入字符串(高效并保留转义字符)
我有两个问题与表示为线方法write(...)
:
- 这也可能是非常低效的。有没有像Matlab中的字符串缓冲区?
- 它将转义序列(如
\n
)写入字符串中,而不是采用其实际含义。
我该如何正确地得到表示线?
代码:
classdef StringTextWriter < Writer
properties
str;
end
methods
function this = StringTextWriter()
% Init the write-target which is a string in our case.
% (Other Writer classes would maybe open a file.)
this.str = '';
end
function write(this, val)
% Write to writer target.
% (Other Writer classes would maybe use fprinf here for file write.)
% ?????????????????????????????
this.str = [this.str val]; % How to do this properly?
% ?????????????????????????????
end
end
end