字符串格式转义序列

本文关键字:转义序列 格式 字符串 | 更新日期: 2023-09-27 17:53:04

我有webmethod从中获取一个记录,并显示在我的asp:label。

我在我的WebMethod中有一个List<string>,在读取数据时,我正在将其转换为String.Format

while(dr.Read())
{
    rst.Add(string.Format("{0}",dr["Practice_short_name"]));
}

然而,当我在标签中显示数据时,我得到了这个:

("CEC2")

但是我希望它是这样的:

CEC2

如何从我的输出中转义括号[]和" ?

字符串格式转义序列

尝试以下代码:

 rst.Add(string.Format("{0}",dr["Practice_short_name"].ToString())

假设数据本身是而不是["VALUE"]形式的,则不需要转义任何内容。字符串。对于一个值替换,格式也不是必需的。

为什么不定义一个扩展方法,也将处理空值在一次移动?

public static string GetText(this DataRow row, string columnName)
{
    if (row.IsNull(columnName))
        return string.Empty;
    return row[columnName] as string ?? string.Empty;
}

那么你可以简单地写:

rst.Add(dr.GetText("Practice_short_name"));