c#从sql 2005返回字符串

c#从sql 2005返回字符串,c#,string,C#,String,嗯 我在sql表中有一个字符串,如下所示 hello /r/n this is a test /r/n new line. 当我使用c#为使用multiline的文本框检索这个字符串时,我希望转义字符是换行符 但事实并非如此,我得到的线与上面的线完全相同 似乎从表返回的字符串被视为文本,但我想要换行符 我怎样才能让它工作 提前感谢../不是转义字符,\n是。您需要的是: 您好\r\n这是一个测试\r\n新行。/不是转义字符,\r\n是。您需要的是: 您好\r\n这是一个测试\r\n新行。像“\

我在sql表中有一个字符串,如下所示

hello /r/n this is a test /r/n new line.
当我使用c#为使用multiline的文本框检索这个字符串时,我希望转义字符是换行符

但事实并非如此,我得到的线与上面的线完全相同

似乎从表返回的字符串被视为文本,但我想要换行符

我怎样才能让它工作


提前感谢..

/不是转义字符,\n是。您需要的是:


您好\r\n这是一个测试\r\n新行。

/不是转义字符,\r\n是。您需要的是:

您好\r\n这是一个测试\r\n新行。

像“\n”(顺便说一下不是“/n”)是编程语言中的转义字符,而不是文本中固有的转义字符。(特别是,它们依赖于编程语言。)

如果你想

hello\r\nthis is a test\r\nnew line
格式为

hello
this is a test
new line
您需要自己进行解析,将“\r”替换为回车符,“\n”替换为换行符等,将“\”处理为文字反斜杠等。我通常发现,一个简单的解析器只要记住前面的字符是否是反斜杠就足够了。大概是这样的:

static string Unescape(string text)
{
    StringBuilder builder = new StringBuilder(text.Length);
    bool escaping = false;
    foreach (char c in text)
    {
        if (escaping)
        {
           // We're not handling \uxxxx etc
           escaping = false;
           switch(c)
           {
               case 'r': builder.Append('\r'); break;
               case 'n': builder.Append('\n'); break;
               case 't': builder.Append('\t'); break;
               case '\\': builder.Append('\\'); break;
               default:
                   throw new ArgumentException("Unhandled escape: " + c);
           }
        }
        else
        {
           if (c == '\\')
           {
               escaping = true;
           }
           else
           {
               builder.Append(c);
           }
        }
    }
    if (escaping)
    {
        throw new ArgumentException("Unterminated escape sequence");
    }
    return builder.ToString();
}
有更有效的方法(从一个反斜杠跳到另一个反斜杠,基本上附加整个非转义文本的子字符串),但这很简单。

像“\n”(不是“/n”)这样的东西是编程语言中的转义字符,而不是文本中固有的。(特别是,它们依赖于编程语言。)

如果你想

hello\r\nthis is a test\r\nnew line
格式为

hello
this is a test
new line
您需要自己进行解析,将“\r”替换为回车符,“\n”替换为换行符等,将“\”处理为文字反斜杠等。我通常发现,一个简单的解析器只要记住前面的字符是否是反斜杠就足够了。大概是这样的:

static string Unescape(string text)
{
    StringBuilder builder = new StringBuilder(text.Length);
    bool escaping = false;
    foreach (char c in text)
    {
        if (escaping)
        {
           // We're not handling \uxxxx etc
           escaping = false;
           switch(c)
           {
               case 'r': builder.Append('\r'); break;
               case 'n': builder.Append('\n'); break;
               case 't': builder.Append('\t'); break;
               case '\\': builder.Append('\\'); break;
               default:
                   throw new ArgumentException("Unhandled escape: " + c);
           }
        }
        else
        {
           if (c == '\\')
           {
               escaping = true;
           }
           else
           {
               builder.Append(c);
           }
        }
    }
    if (escaping)
    {
        throw new ArgumentException("Unterminated escape sequence");
    }
    return builder.ToString();
}

有更有效的方法(从一个反斜杠跳到另一个反斜杠,基本上附加整个非转义文本的子字符串),但这很简单。

前几天我的SQLite数据库也有类似的问题。用户可以在文本框中输入多行文本,将其插入数据库时,会显示如下内容:

"This is \r\n a multiline test"
它来自
System.Data.SQLite
as:

"This is \\r\\n a multiline test"
我最终使用了:

String.Replace("\\r","\r").Replace("\\n","\n") 

替换在多行文本框或标签中显示的文本格式正确的每个转义字符。

前几天,我的SQLite数据库也出现了类似问题。用户可以在文本框中输入多行文本,将其插入数据库时,会显示如下内容:

"This is \r\n a multiline test"
它来自
System.Data.SQLite
as:

"This is \\r\\n a multiline test"
我最终使用了:

String.Replace("\\r","\r").Replace("\\n","\n") 

替换在多行文本框或标签中显示的文本格式正确的每个转义字符。

看起来您需要在字符串上使用System.Web.HttpUtility.HtmlCode,然后再将其放入文本框中

 textBox.Value = HttpUtility.HtmlDecode(myString);

在将字符串放入文本框之前,似乎需要在字符串上使用System.Web.HttpUtility.HtmlCode

 textBox.Value = HttpUtility.HtmlDecode(myString);

我不明白为什么在这个例子中需要解析器,因为所讨论的语言是C#,并且我假设操作系统是32位窗口,它将
\r\n
视为换行符

以下简单代码(Windows应用程序)适用于我:

string s = "hello \r\n this is a test \r\n new line.";

// Set Multiline to True.
textBox1.Multiline = true;

// Expand the height of the textbox to be able to view the full text.
textBox1.Height = 100;
textBox1.Text = s;
文本框将文本显示为:

hello
 this is a test 
 new line.

我不明白为什么在这个例子中需要解析器,因为所讨论的语言是C#,并且我假设操作系统是32位窗口,它将
\r\n
视为换行符

以下简单代码(Windows应用程序)适用于我:

string s = "hello \r\n this is a test \r\n new line.";

// Set Multiline to True.
textBox1.Multiline = true;

// Expand the height of the textbox to be able to view the full text.
textBox1.Height = 100;
textBox1.Text = s;
文本框将文本显示为:

hello
 this is a test 
 new line.

我认为这里的问题是,用户看到的字符串是

您好\r\n这是一个测试\r\n新行

而C#代码将其视为

"hello \\r\\n this is a test \\r\\n new line."
一个更简单的解决方案如下

s = Regex.Replace(s, @"\\\\", @"\");

将@“\\”替换为@“\”。不要被正则表达式匹配模式弄糊涂,必须在匹配中转义\而不是替换。

我认为这里的问题是用户看到的字符串是

您好\r\n这是一个测试\r\n新行

而C#代码将其视为

"hello \\r\\n this is a test \\r\\n new line."
一个更简单的解决方案如下

s = Regex.Replace(s, @"\\\\", @"\");

将@“\\”替换为@“\”。不要被正则表达式匹配模式弄糊涂,必须在匹配中转义\而不是替换。

对不起,我的错误,字符串是hello\r\n这是一个测试\r\n新行。还是一样的问题!抱歉,我的错误,字符串是hello\r\n这是一个测试\r\n新行。还是一样的问题!字符串应为hello\r\n这是一个测试\r\n新行。我得到了错误的转义符,我仍然得到了问题字符串应该是hello\r\n这是一个测试\r\n新行。我把转义符搞错了,我还是有问题编程语言是C,但文本不是来自C源文件,而是来自数据库。您的案例中还有一个解析器—C#编译器。@Cerburus—我想我的帖子可能会解释为什么您的代码示例工作正常。毫无疑问,这是可能的,但OP有责任提供准确的信息,而不是让我们猜测。编程语言是C#,但是文本不是来自C源文件,而是来自数据库。您的案例中还有一个解析器—C#编译器。@Cerburus—我想我的帖子可能会解释为什么您的代码示例工作正常。毫无疑问,这是可能的,但OP有责任提供准确的信息,而不是让我们猜测。如果有人想输入“old\new”,可能会以“old\\new”结尾,会发生什么在数据库中?:)很好的观点——我没有想到:)谢谢你指出这一点!如果有人想输入“old\new”,可能会在数据库中以“old\\new”结尾,该怎么办?:)很好的观点——我没有想到的:)谢谢你指点