如何在Ada中创建一个包含换行符的字符串,其定义也包含那些换行符?
我已经尝试过在行尾使用0..2反斜杠,但是没有一个可以编译:
usage_info : String := "\
This should be the first line
and both the definition and the output
should contain newlines.";
在PHP中为:
<<<BLOCK
1
2
3
BLOCK;
const std::string s = "\
1
2
3";
const string s =
@"1
2
3";
据我所知,Ada与Java一样,不支持多行文字。我唯一看到的就是这样:
usage_info : String := "This should be the first line" & CR & LF
& "and both the definition and the output" & CR & LF
& "should contain newlines.";
当然,您需要with和use Ada.Characters.Latin_1使这些常量可见。
对弗雷德里克·普拉卡(FrédéricPraca)的补充:
根据您的需要,您可以使用ASCII
包代替Ada.Characters.*
(例如Latin_1,Latin_9,Wide_Latin_ ..等)。由于ASCII
不是包,因此无法进行with
修改,因此您必须为所有内容加上前缀(或使用renames
定义“别名”)
declare
flex : constant String := "Foo" & ASCII.CR & "bar" & ASCII.LF;
flux : constant String := "Foo" & ASCII.CR
& "bar" & ASCII.LF;
begin
-- do stuff
null;
end;
一个人可以定义一个自定义&
运算符,以将其用作新的行插入点。但是...它有多有用?
function Foo (Left, Right : String) return String renames "&";
function Boo (Left : String; Right : Character) return String renames "&";
function "&" (Left, Right : String) return String is begin
return Foo (
Boo (Left, ASCII.LF),
Right);
end "&";
Ada.Text_IO.Put_Line("Foo" &
"bar");