区分 dart 字符串中的换行符

问题描述 投票:0回答:1

我有以下代码:

const myString = """
BEGIN:VCALENDAR
PRODID:-//xyz Corp//NONSGML PDA Calendar Version 1.0//EN
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:19960704T120000Z
UID:[email protected]
ORGANIZER:mailto:[email protected]
DTSTART:19960918T143000Z
DTEND:19960920T220000Z
STATUS:CONFIRMED
CATEGORIES:CONFERENCE
SUMMARY:Networld+Interop Conference
DESCRIPTION:Networld+Interop Conference and Exhibit\n Atlanta World Atlanta, Georgia
END:VEVENT
END:VCALENDAR
""";

注意

\n
属性中的
DESCRIPTION
。 现在我想将字符串分成几行。使用
myString.split("\n")
时, 它还分割了描述行。 我怎样才能告诉 dart 只分割“真正的”换行符?

flutter dart
1个回答
0
投票

您可以通过在左引号前添加

myString
r 声明为
原始字符串

const myString = r"""
BEGIN:VCALENDAR
PRODID:-//xyz Corp//NONSGML PDA Calendar Version 1.0//EN
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:19960704T120000Z
UID:[email protected]
ORGANIZER:mailto:[email protected]
DTSTART:19960918T143000Z
DTEND:19960920T220000Z
STATUS:CONFIRMED
CATEGORIES:CONFERENCE
SUMMARY:Networld+Interop Conference
DESCRIPTION:Networld+Interop Conference and Exhibit\n Atlanta World Atlanta, Georgia
END:VEVENT
END:VCALENDAR
""";

void main() {
  myString.split("\n").forEach(print);
}

哪个输出:

BEGIN:VCALENDAR
PRODID:-//xyz Corp//NONSGML PDA Calendar Version 1.0//EN
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:19960704T120000Z
UID:[email protected]
ORGANIZER:mailto:[email protected]
DTSTART:19960918T143000Z
DTEND:19960920T220000Z
STATUS:CONFIRMED
CATEGORIES:CONFERENCE
SUMMARY:Networld+Interop Conference
DESCRIPTION:Networld+Interop Conference and Exhibit\n Atlanta World Atlanta, Georgia
END:VEVENT
END:VCALENDAR
© www.soinside.com 2019 - 2024. All rights reserved.