Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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/9/google-apps-script/5.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#_Regex_String_Foreach - Fatal编程技术网

如何通过C#更有效地操纵字符串?

如何通过C#更有效地操纵字符串?,c#,regex,string,foreach,C#,Regex,String,Foreach,原始字符串 它由三部分组成: 1-单词(中文)2-[img]图片url[/img]3- [url]url[/url]4- 结果如下: SomeTextSomeTextSomeTextSomeText[img]http://www.a.com/a.jpg[/img]其他文本其他文本[img]http://c.b.com/a.jpg[/img]anothertextanothertextanothertextanothertextanothertextanothertext[url]http://

原始字符串
它由三部分组成:

1-单词(中文)
2-[img]图片url[/img]
3- [url]url[/url]
4-



结果如下:

SomeTextSomeTextSomeTextSomeText
[img]http://www.a.com/a.jpg[/img]其他文本其他文本[img]http://c.b.com/a.jpg[/img]anothertextanothertextanothertextanothertextanothertextanothertext[url]http://d.e.f[/url]alwaystext[img]http://f.g.com/a.gif[/img][img]http://d.e.net/a.png[/img]

我想要什么
仍然由3部分组成,但有一点变化:

1-单词
2-
3-url


我现在做什么

//内容是源字符串
字符串[]contentArray=Regex.Split(内容,@“\[img\]”(.+?)\[/img\]”,RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
StringBuilder sb=新的StringBuilder();
foreach(contentArray中的字符串项)
{
//如果是pic url
if(item.StartsWith(“http://”,StringComparison.OrdinalIgnoreCase)&
(项目.EndsWith(“.jpg”,StringComparison.OrdinalIgnoreCase)||
item.EndsWith(“.gif”,StringComparison.OrdinalIgnoreCase)||
item.EndsWith(“.png”,StringComparison.OrdinalIgnoreCase)))
{
//将其转换为链接
//附属于某人
}
其他的
{
string[]contentArray1=Regex.Split(项,@“\[url\]”(.+?)\[/url\]”,RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach(ContentArray中的字符串tmpItem)
{
如果(它是一个URL)
{
//将其转换为链接
//附属于某人
}
else//必须是普通文本
{
//做一个更好的布局
//放入一个
//附属于某人
}
}
}
}
问题

上面的代码是有效的,但是,还有更好的解决方案吗?为了提高效率,我这里所说的“更好的解决方案”意味着更快的速度,一种编码方式是使用Match。替换为Match Evaluator委托。请注意,在访问[url]和[img]的match.Groups项目时有不同的索引,因为它们对应于原始注册表中的不同组。表情

    public static string ReplaceTags(Match match)
    {
        if (match.Value.StartsWith("[url]") && match.Groups.Count == 4)
            return String.Format("<a href=\"{0}\">{0}</a>", match.Groups[2]);
        else if (match.Value.StartsWith("[img]") && match.Groups.Count == 4)
            return String.Format("<img src=\"{0}\" width=\"300\">", match.Groups[3]);

        throw new Exception("Unknown match found. Deal with it.");
    }

    static void Main(string[] args)
    {
        string text = "text[url]http://some_url[/url]text2[img]http://some/img.jpg[/img]text3";

        Regex regex = new Regex(@"(\[url](.*)\[/url]|\[img](.*)\[/img])");

        string result = regex.Replace(text, ReplaceTags);

        Console.WriteLine(result);
    }
公共静态字符串替换标记(匹配)
{
if(match.Value.StartsWith(“[url]”)和&match.Groups.Count==4)
返回String.Format(“,match.Groups[2]);
else if(match.Value.StartsWith(“[img]”)和&match.Groups.Count==4)
返回String.Format(“,match.Groups[3]);
抛出新异常(“找到未知匹配项。请处理它”);
}
静态void Main(字符串[]参数)
{
string text=“text[url]http://some_url[/url]text2[img]http://some/img.jpg[/img]text3”;
Regex Regex=new Regex(@“(\[url](.*)\[/url]|\[img](.*)\[/img])”;
字符串结果=regex.Replace(文本,ReplaceTags);
控制台写入线(结果);
}

那么您的解决方案是否有效?如果不行,那怎么办?如果不行,你希望改进什么?@Servy我认为它行得通,但OP想知道,它是否能更快。@Nolonar不清楚它是否行得通,这就是为什么我问的原因,也不清楚他想如何改进它,他没有试图说明这一点。他可能希望它运行得更快,使用更少的内存,更具可读性,处理当前无法处理的输入,使用更少的代码,或者其他任何因素。我认为@Servy指出的主要问题是“更好”是相对的。OP-您应该澄清您想要改进的地方。@Servy抱歉,我已经编辑了OP,上面的代码已经运行,但是我想知道是否有更好的解决方案来提高效率
    public static string ReplaceTags(Match match)
    {
        if (match.Value.StartsWith("[url]") && match.Groups.Count == 4)
            return String.Format("<a href=\"{0}\">{0}</a>", match.Groups[2]);
        else if (match.Value.StartsWith("[img]") && match.Groups.Count == 4)
            return String.Format("<img src=\"{0}\" width=\"300\">", match.Groups[3]);

        throw new Exception("Unknown match found. Deal with it.");
    }

    static void Main(string[] args)
    {
        string text = "text[url]http://some_url[/url]text2[img]http://some/img.jpg[/img]text3";

        Regex regex = new Regex(@"(\[url](.*)\[/url]|\[img](.*)\[/img])");

        string result = regex.Replace(text, ReplaceTags);

        Console.WriteLine(result);
    }