Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 定界符替换方法_C#_Asp.net - Fatal编程技术网

C# 定界符替换方法

C# 定界符替换方法,c#,asp.net,C#,Asp.net,我想创建一个方法来替换用于预期目标用途(html电子邮件、日志、数据库)的分隔符。分隔符是常量,因此我希望能够引用将可识别名称映射到字符串值的对象(分号=“;”、htmlLineBreak=“”等)。有没有比下面更好的方法 public static class Utilities { public string ReplaceDelimiter(string content , Delimiter currentDelimiter, Delim

我想创建一个方法来替换用于预期目标用途(html电子邮件、日志、数据库)的分隔符。分隔符是常量,因此我希望能够引用将可识别名称映射到字符串值的对象(分号=“;”、htmlLineBreak=“
”等)。有没有比下面更好的方法

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


当前的实现有什么问题?为什么要传递静态类?它们都是“电流”或“输出”——没有区别。除此之外,你可以考虑字典、迭代键和按值替换,但不清楚如何更好地为你做这件事。你想要一些能应付“A、B、C”、“D”或A、B、C、D的东西吗?如果没有,您所拥有的就可以了。使用Environment.NewLine而不是\r\n我会认为您希望将数据保存在某种类型的类中,然后将其序列化为不同的格式。停止用“分隔符”来思考。
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);