【问题标题】:Find each index within string & store the indexes separately在字符串中查找每个索引并分别存储索引
【发布时间】:2016-07-14 07:43:01
【问题描述】:

我有一个字符串,它基本上包含一个 XML 文件,带有标签和所有内容。 我特别感兴趣的一个标签是<address source>。 问题是,XML 文件并不总是相同的。有时可能只有一个标签 <address source> 存在,有时可能存在 5 个标签,有时甚至 20 个或更多。

所以,想象一下我的字符串是这样的:

string XMLToAnalyze = "<XML><TAG1>somecontent</TAG1><address source>content</address source><address source>content</address source><TAG2>morecontent</TAG2></XML>"`

所以,在这个特定的字符串中,标签&lt;address source&gt; 有两倍。

我需要的是这个:

我需要找到每个标签&lt;address source&gt; 的索引(或IndexOf),并且我需要将这些索引单独存储,最好是真正以单独的整数(每个索引一个整数)存储,或者存储在一个数组中。这是因为我需要访问每个单独的整数来填写 Winforms 表单中的某些字段。

这可能吗?

【问题讨论】:

  • post中的xml错误。在节点
    中,source是一个属性吗?
  • 您引用的 XML 无效。 &lt;address source&gt; 是具有空 source 属性的 Address 节点。您可能希望找到源头来解决这个可怕的疏忽。
  • 你确定你不想把 XML 当作……也就是 XML?
  • 为什么需要每个节点的索引?还是您只需要 XML 中存在的顺序?

标签: c# string indexing


【解决方案1】:

使用正则表达式匹配所有字符串,然后遍历匹配项并找到每个匹配项的索引。

这个逻辑必须有效。这是Tested

        List<int> indexes = new List<int>();
        string XMLToAnalyze = "<XML><TAG1>somecontent</TAG1><address source>content</address source><address source>content</address source><TAG2>morecontent</TAG2></XML>";
        var regex = new Regex(@"<address source>");

        foreach (Match match in regex.Matches(XMLToAnalyze))
        {
            indexes.Add(match.Index);
        }

indexes 将包含匹配字符串的所有索引。


输出: 29、69

【讨论】:

  • 谢谢,这是我熟悉的代码。不过有一个问题,我将如何在文本框中显示indexes
  • 您在使用网络表单吗? MV?或者是其他东西?你在哪里有文本框
  • Windows 窗体是我正在使用的。但我想我已经找到了:我使用string.join(environment.newline, indexes) 在多行字段中显示索引。不过,另一个问题是:列表相当新:例如,我将如何提取列表中的第二项?
  • 通过使用索引。 say like List[0] 会给你列表中的第一项,其中ListList&lt;&gt; 类型的变量
  • @Reddy 不建议在 XML 上使用正则表达式 stackoverflow.com/questions/1732348/…
【解决方案2】:

你需要的是一个 IDictionary>。 当您搜索开始标签时,您可以通过以下方式将它们存储在字典中: 如果标签存在,则将新找到的索引添加到添加的列表中,否则将新元素添加到字典中,其中包含一个包含单个项目的新列表 - 索引的第一次出现。在您浏览完所有字符串之后 - 您的字典将包含您正在查找的地图。

public static class XmlTagMapBuilder
{
    public static IDictionary<string, IList<int>> GetOpenTagIndexMap(string inputXml)
    {
        // Argument validation goes here

        IDictionary<string, IList<int>> result = new Dictionary<string, IList<int>>();

        int currentIndex = -1;
        string lastOpenTag = null;
        while (true)
        {
            string nextOpenTagName;
            int nextOpenTagIndex;
            if (TryGetNextOpenTagIndex(inputXml, currentIndex, out nextOpenTagName, out nextOpenTagIndex))
            {
                lastOpenTag = nextOpenTagName;
                currentIndex = nextOpenTagIndex;

                IList<int> tagIndicies;
                if (!result.TryGetValue(nextOpenTagName.ToUpperInvariant(), out tagIndicies))
                {
                    tagIndicies = new List<int>();
                    result.Add(nextOpenTagName, tagIndicies);
                }

                tagIndicies.Add(nextOpenTagIndex);
            }
            else
            {
                break;
            }
        }

        return result;
    }

    /// <summary>
    /// Tries to get next open tag in the given <see cref="inputXml"/> string after the specified startIndex.
    /// </summary>
    /// <param name="inputXml">The string which contains the xml tags.</param>
    /// <param name="startIndex">The index after which to look for the open tag.</param>
    /// <param name="nextOpenTagName">If a tag was found, contains its name.</param>
    /// <param name="nextOpenTagIndex">If a tag was found, contains the start index of it.</param>
    /// <returns>true - if the tag was found. false - otherwise.</returns>
    private static bool TryGetNextOpenTagIndex(string inputXml, int startIndex, out string nextOpenTagName, out int nextOpenTagIndex)
    {
        // Need to add implementaiton here
    }
}

【讨论】:

  • 我对字典一点也不熟悉。是否可以向我展示如何结合我的字符串执行此操作的示例?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多