gpt4 book ai didi

c# - 如何在多行字符串中找到分布在多行的句子

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:00 25 4
gpt4 key购买 nike

正如标题所说。我把这个字符串从一个文件读入一个字符串

"has anyone really been far even as decided to
use even go want
to do look more like"

当我这样做时,它会写出 hello world。

    if (string.Contains(@"use even go want
to do look more like"))
{
Console.Write("Hello world!");
}

但是当我这样做时,它不会起作用。

if (string.Contains(@"use even go want\nto do look more like"))
{
Console.Write("Hello world!");
}

有没有更好的方法来查找字符串中分布在多行的句子,而不是仅仅在代码中间插入一个回车?

(我也已经尝试过 \n\r\r\n\r)

最佳答案

这是因为 \nverbatim string literal 中将被解释为 '\' 后跟 'n' .从字符串文字中删除 @ 应该可以解决问题:

if (string.Contains( "use even go want\nto do look more like"))
{ // ^
Console.Write("Hello world!");
}

或者如果分隔符是系统特定的,您可以使用 System.Environment.NewLine:

if (string.Contains("use even go want"+System.Environment.NewLine+"to do look more like"))
{
Console.Write("Hello world!");
}

关于c# - 如何在多行字符串中找到分布在多行的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29371017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com