flutter 将包含大量引号的文本放入String中

roejwanj  于 2022-12-27  发布在  Flutter
关注(0)|答案(1)|浏览(252)

我有一个很长的文本,当我试图把它放在一个字符串变量内的引号或双引号发生错误,因为文本包含大量的单引号和双引号,其中一些是不封闭的,有没有解决办法,除了删除文本中的引号?
我用扑动。
这是经文:

Once upon a time, in a small village nestled in the rolling hills of the countryside, there lived a young boy named Jack. Jack was a curious and adventurous boy who loved to explore the fields and forests around his village.
One day, as he was walking through the woods, he came upon a beautiful clearing filled with wildflowers. In the center of the clearing stood a large, old oak tree. As Jack walked closer to the tree, he noticed a small door set into the trunk.
Intrigued, Jack pushed open the door and peered inside. To his surprise, he saw a tiny, cozy room filled with all sorts of treasures: shiny jewels, golden coins, and sparkling crystals. Jack couldn't believe his eyes!
As he reached out to touch the glittering jewels, a soft voice spoke from behind him. "Who are you, and what are you doing in my tree?"
Startled, Jack turned around to see a tiny fairy with shimmering wings and a bright smile. "I'm sorry," said Jack, bowing his head. "I didn't mean to intrude. I was just exploring and I saw the door and couldn't resist coming in."
The fairy chuckled and flew closer to Jack. "There's no need to apologize, dear boy," she said. "I'm glad you found my tree. It's not often that humans stumble upon my home. My name is Luna, and I'm the guardian of this forest."
Jack's eyes widened in amazement. "A guardian fairy! I've never met one before. Can you grant me wishes, like in the stories?"
Luna laughed and shook her head. "I'm afraid not, dear Jack. But I can give you a gift, if you'd like. Choose something from my treasure trove to take with you on your adventures."
Jack thought for a moment, then reached for a small, shimmering crystal. "This one, please," he said, holding it up for Luna to see.
"Ah, a wise choice," said Luna, smiling. "That crystal will bring you good luck and protect you on your travels."
With a grateful heart, Jack thanked Luna and promised to take good care of the crystal. And as he left the tree and made his way back through the woods, he felt a sense of magic and wonder surrounding him, knowing that he had made a new friend in the magical guardian of the forest.
9wbgstp7

9wbgstp71#

是的,有一种方法可以将包含引号的字符串包含在Dart中。您可以使用反斜杠()字符来转义引号,如下所示:

String str = "This is a string that contains \"quotes\" within it.";

或者,您可以使用单引号定义字符串,然后在字符串中使用双引号而不转义它们:

String str = 'This is a string that contains "quotes" within it.';

您还可以使用三重引号(单引号或双引号)来定义多行字符串。(哪种方法最适合您)
例如:

String str = """
This is a
multi-line string
that contains quotes "" within it.
""";

这些技术将允许您在字符串中包含引号,而不会导致语法错误。

相关问题