如何删除标签之间的值? [英] How do I delete the values in between the tags?

查看:32
本文介绍了如何删除标签之间的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除标签内的值,我该怎么做?当我加载保存文件时,突出显示的输出是我想要删除的内容.

I want to delete the values inside the tags, how can I do that? the highlighted output is what I want to delete when I load the save the file.

 <data name="Enrolment_Exit_Verify_Message" d2p1:space="preserve"  xmlns:d2p1="http://www.w3.org/XML/1998/namespace">
<value**>**Are you sure you want to Exit without Saving?****</value>
<comment>[Font]**Italic**[/Font][DateStamp]2015/02/01 00:00:00[/DateStamp][Comment] **this is a My awesome new comments comment.!!**[/Comment]</comment>

这是我在标签之间阅读的方式?我不知道如何删除标签之间的内容.

Here is how I have read in between the tags? I don't know how can I delete in-between the tags.

            for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
            {
                string comment = oDataSet.Tables["data"].Rows[i][2].ToString();

                string font = Between(comment, "[Font]", "[/Font]");
                string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
                string commentVal = Between(comment, "[Comment]", "[/Comment]");

                string[] row = new string[] 
                {
                    oDataSet.Tables["data"].Rows[i][0].ToString(),
                    oDataSet.Tables["data"].Rows[i][1].ToString(),
                    font, 
                    datestamp, commentVal };
                Gridview_Output.Rows.Add(row);
            }
            oDataSet.Tables.Add(oDataTable);

字符串函数

    public string Between(string STR, string FirstString, string LastString)
    {
        string FinalString;
        int Pos1 = STR.IndexOf(FirstString) + FirstString.Length;
        int Pos2 = STR.IndexOf(LastString);
        FinalString = STR.Substring(Pos1, Pos2 - Pos1);
        return FinalString;
    }

推荐答案

如果您可以构建正则表达式,则可以在 between 方法中使用正则表达式.这里有更多信息正则表达式

You can go with the regex in your between method if you can build up the regex. Here is more info on the regex

     public string Between(string STR, string FirstString, string LastString)
     {
         string regularExpressionPattern1 = @"(?:\" + FirstString + @")([^[]+)\[\/" + LastString;
        Regex regex = new Regex(regularExpressionPattern1, RegexOptions.Singleline);
        MatchCollection collection = regex.Matches(STR.ToString());
        var val = string.Empty;
        foreach (Match m in collection)
        {
            val = m.Groups[1].Value;
        }
        return val;
     }

注意- 未测试的代码可能需要根据您的需要进行调整.测试了 Althoguh 正则表达式.

Note- Code is not tested may need to tweak for your need . Althoguh regex expression is tested.

这里是正则表达式的工作小提琴

在执行函数后,上面的代码为您提供了来自标签的值,变量将具有值

The above code give you the values from the tags in you r case after executing the functions the variables will have the values

   font = "**Italic**"  
   datestamp = "2015/02/01 00:00:00"
   commentVal = "**this is a My awesome new comments comment.!!**"

然后,如果您希望它从 comment 变量中删除,只需使用 Replaceas

Then if you want it to remove from the comment variable just use Replaceas

  comment = comment.Replace(font,string.Empty);
  comment = comment.Replace(datestamp ,string.Empty);
  comment = comment.Replace(commentVal ,string.Empty);

在它的末尾,您将拥有 comment 变量,其中包含从该标签中删除的值.

At the end of it you will have the comment variable with removed values from that tags.

这篇关于如何删除标签之间的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆