【问题标题】:How do I delete the values in between the tags?如何删除标签之间的值?
【发布时间】:2015-02-11 06:29:24
【问题描述】:

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

 <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>

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

            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 方法使用了 Substring。请尝试使用RemoveRange。如果这对您没有意义,也许也可以发布Between 方法的代码供我们查看。
  • 我已经添加了代码...
  • 你试过了吗?
  • 再次查看您的代码,一切似乎都合乎逻辑。您将“中间”放入适当的变量中,之后您甚至不使用 comment 变量。您为什么以及在哪里尝试从完整字符串中删除中间值?

标签: c# winforms tags


【解决方案1】:

如果您可以构建正则表达式,您可以在 between 方法中使用正则表达式。 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 正则表达式经过测试。

Here is working fiddle for regex

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

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

如果你想从comment 变量中删除它,只需使用Replaceas

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

最后,您将拥有 comment 变量,其中已从该标签中删除了值。

【讨论】:

  • 可以肯定地假设 OP 是新手。不应该给他复杂的正则表达式,解释它是什么,因为这只会让他感到困惑。至少,链接到有关正则表达式的资源,即便如此,我认为也可以加入“简单的解决方案”。
  • 我同意你的看法,但是,使用字符串函数在字符串中追踪你想要的内容比regex 更难,更复杂,此外,如果你不尝试新的你学不到的东西。我认为学习新东西很好。
  • 学习新东西很棒。总是。但是,如果你得到太多,一切都会变得糟糕。在这里,我看到太多信息而解释太少。此外,在没有regex 的情况下运行它可能需要做更多的工作,但基本的字符串函数仍然是基础,这就是你开始的地方。 OP 已经弄清楚了大部分,他只是指导如何使他的Between 方法具有“删除”逻辑而不是“substr”逻辑。
  • 所以换句话说,我必须删除读取中间字符串的方式?我现在很困惑,我只想删除两者之间的值
  • 您可以像@YoryeNathan 提到的那样尝试string methods 并找到标签并替换它(这可能会有点复杂)。或者您可以使用我在答案中给出的方法。为了进一步了解,您可以在线阅读资源并进行调试以了解其工作原理。
【解决方案2】:

要删除中间的值,可以修改Code的@Coder提供的正则表达式,如下所示:

        string str = "[Font]**Italic**[/Font][DateStamp]2015/02/01 00:00:00[/DateStamp][Comment] **this is a My awesome new comments comment.!!**[/Comment]";
        string strPattern = @"(?:])([^[]+)\["; // Regular expression pattern to find string inside ][
        Regex rg = new Regex(strPattern);
        string newStr = rg.Replace(str, string.Format("]{0}[",string.Empty), int.MaxValue);

【讨论】:

  • 它正在破坏字符串 deletestr = "[Value][/Value][Font][/Font],[DateStamp][/DateStamp],[Comment][/Comment]";字符串 strPattern = @"(?:])([^[]+)["; // 查找字符串的正则表达式模式 ][ Regex rg = new Regex(strPattern,RegexOptions.Singleline); string newStr = rg.Replace(deletestr, string.Format("]{0}[", string.Empty), int.MaxValue); MatchCollection 集合 = rg.Matches(Value.ToString());匹配 m = 集合[0]; //在这里中断返回 m.Groups[1].Value;
猜你喜欢
  • 2016-05-08
  • 2010-11-24
  • 2015-12-26
  • 2012-01-17
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
相关资源
最近更新 更多