C# 定界符替换方法
我想创建一个方法来替换用于预期目标用途(html电子邮件、日志、数据库)的分隔符。分隔符是常量,因此我希望能够引用将可识别名称映射到字符串值的对象(分号=“;”、htmlLineBreak=“C# 定界符替换方法,c#,asp.net,C#,Asp.net,我想创建一个方法来替换用于预期目标用途(html电子邮件、日志、数据库)的分隔符。分隔符是常量,因此我希望能够引用将可识别名称映射到字符串值的对象(分号=“;”、htmlLineBreak=“”等)。有没有比下面更好的方法 public static class Utilities { public string ReplaceDelimiter(string content , Delimiter currentDelimiter, Delim
”等)。有没有比下面更好的方法
public static class Utilities
{
public string ReplaceDelimiter(string content
, Delimiter currentDelimiter, Delimiter outputDelimiter)
{
return content.Replace(currentDelimiter.ToString()
, outputDelimiter.ToString());
}
}
public class Delimiter
{
public const string comma = ",";
public const string semicolon = ";";
public const string colon = ":";
public const string lineBreak = "\r\n";
public const string htmlLineBreak = "<br/>";
}
- 代码
public class Delimiter { public static readonly Delimiter HtmlLineBreak=new Delimiter { Value="<br/>" }, LineBreak=new Delimiter { Value="\r\n" }, Semicolon=new Delimiter { Value=";" }, Colon=new Delimiter { Value=":" }, Comma=new Delimiter { Value="," }; public override String ToString() { return Value; } public String Value { get; set; } }
- 输出
123
public class Delimiter {
public static readonly Delimiter
HtmlLineBreak=new Delimiter {
Value="<br/>"
},
LineBreak=new Delimiter {
Value="\r\n"
},
Semicolon=new Delimiter {
Value=";"
},
Colon=new Delimiter {
Value=":"
},
Comma=new Delimiter {
Value=","
};
public override String ToString() {
return Value;
}
public String Value {
get;
set;
}
}
var t=Utilities.ReplaceDelimiter("123\r\n", Delimiter.LineBreak, Delimiter.HtmlLineBreak);
Debug.Print("{0}", t);