C# 在数组中查找文本 我把一个例程从C++移植到C语言,很难理解端口为什么失败:

C# 在数组中查找文本 我把一个例程从C++移植到C语言,很难理解端口为什么失败:,c#,C#,我有一个字符串数组,其中包含我正在删除的内容 string[] aryLines = File.ReadAllLines(mstrFilename); 该数组包含以下内容: aryLines = new[] { "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "<!--", "", " File:\t\tuif.xml, User Interface", " Notes:\tThi

我有一个字符串数组,其中包含我正在删除的内容

    string[] aryLines = File.ReadAllLines(mstrFilename);
该数组包含以下内容:

aryLines = new[]
{
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>",
    "<!--",
    "",
    "  File:\t\tuif.xml, User Interface",
    "  Notes:\tThis file contains the application layout and includes",
    "\t\tfor other files defining the application look and functionality.",
    "",
    "  Node:\t\tuif, Root container node",
    "  Attributes:\tid\t\t: Unique node identifier",
    "\t\tcameras\t\t: Initial camera set-up",
    " \t\tcolor_bg\t: Application background colour:",
    "\t\t\t\t\tAlpha, Red, Green, Blue",
    "\t\theight\t\t: Height of the application container",
    "\t\twidth\t\t: Width of the appplication container\t\t",
    "",
    "  Node:\t\tinclude, Includes another XML file",
    "  Attributes:\tname\t\t: Encoded path to XML file to include",
    "",
    "  History:\t2017/09/11 Created by Simon Platten",
    "// -->"
};

上面的例程还没有完成,但是在调试器intClose中观察总是-1,为什么?

这里的问题是
数组。IndexOf
将每个元素与您要查找的元素进行比较,只是您要查找的元素实际上不在数组中。数组中的内容是
/-->
,而不仅仅是
-->

您可以创建一个简单的函数,如下所示:

private int IndexOfInArray(string[] array, string elemToFind, int startIndex = 0)
{
    for (int i = startIndex; i < array.Length; i++)
    {
        if (array[i].Contains(elemToFind))
            return i;
    }

    return -1;
}
int intOpen = 0;
while((intOpen = IndexOfInArray(aryLines, msrostrCmtOpen, intOpen)) >= 0 ) 
{
    //Opening marker located, look for closing marker   
    int intClose = IndexOfInArray(aryLines, msrostrCmtClose, intOpen);
    if (intClose < intOpen) 
    {
        //Shouldn't get here!
        continue;
    }

    Console.WriteLine(intOpen);
}
    string strXML = File.ReadAllText(mstrFilename);

    int intOpen = 0;
    while( (intOpen = strXML.IndexOf(msrostrCmtOpen, intOpen)) >= 0 ) {
    //Opening marker located, look for closing marker   
            int intClose = strXML.IndexOf(msrostrCmtClose, intOpen);

            if ( intClose < intOpen ) {
    //Shouldn't get here!
                continue;
            }
            Console.WriteLine(intOpen);
        }
private int IndexOfInArray(字符串[]数组,字符串elemtofid,int startIndex=0)
{
for(int i=startIndex;i
然后像这样使用它:

private int IndexOfInArray(string[] array, string elemToFind, int startIndex = 0)
{
    for (int i = startIndex; i < array.Length; i++)
    {
        if (array[i].Contains(elemToFind))
            return i;
    }

    return -1;
}
int intOpen = 0;
while((intOpen = IndexOfInArray(aryLines, msrostrCmtOpen, intOpen)) >= 0 ) 
{
    //Opening marker located, look for closing marker   
    int intClose = IndexOfInArray(aryLines, msrostrCmtClose, intOpen);
    if (intClose < intOpen) 
    {
        //Shouldn't get here!
        continue;
    }

    Console.WriteLine(intOpen);
}
    string strXML = File.ReadAllText(mstrFilename);

    int intOpen = 0;
    while( (intOpen = strXML.IndexOf(msrostrCmtOpen, intOpen)) >= 0 ) {
    //Opening marker located, look for closing marker   
            int intClose = strXML.IndexOf(msrostrCmtClose, intOpen);

            if ( intClose < intOpen ) {
    //Shouldn't get here!
                continue;
            }
            Console.WriteLine(intOpen);
        }
int intOpen=0;
而((intOpen=IndexOfInArray(aryLines,msrostrCmtOpen,intOpen))>=0)
{
//找到打开标记,查找关闭标记
int intClose=IndexOfInArray(aryLines、msrostrCmtClose、intOpen);
如果(intClose

您可能必须将
intOpen
设置为
intClose
,或者只将
intOpen++
设置为永远不会得到相同的结果。

我现在已经解决了这个问题,多亏了注释中的建议,新的解析器如下所示:

private int IndexOfInArray(string[] array, string elemToFind, int startIndex = 0)
{
    for (int i = startIndex; i < array.Length; i++)
    {
        if (array[i].Contains(elemToFind))
            return i;
    }

    return -1;
}
int intOpen = 0;
while((intOpen = IndexOfInArray(aryLines, msrostrCmtOpen, intOpen)) >= 0 ) 
{
    //Opening marker located, look for closing marker   
    int intClose = IndexOfInArray(aryLines, msrostrCmtClose, intOpen);
    if (intClose < intOpen) 
    {
        //Shouldn't get here!
        continue;
    }

    Console.WriteLine(intOpen);
}
    string strXML = File.ReadAllText(mstrFilename);

    int intOpen = 0;
    while( (intOpen = strXML.IndexOf(msrostrCmtOpen, intOpen)) >= 0 ) {
    //Opening marker located, look for closing marker   
            int intClose = strXML.IndexOf(msrostrCmtClose, intOpen);

            if ( intClose < intOpen ) {
    //Shouldn't get here!
                continue;
            }
            Console.WriteLine(intOpen);
        }
string strXML=File.ReadAllText(mstrFilename);
int intOpen=0;
而((intOpen=strXML.IndexOf(msrostrCmtOpen,intOpen))>=0){
//找到打开标记,查找关闭标记
int intClose=strXML.IndexOf(msrostrCmtClose,intOpen);
如果(intClose
由于注释可以跨越多行,您可能应该将文件读入一个长字符串,而不是一系列独立的“行”。那么您的逻辑就更清晰了。因为您在第19行看到了//-->数组。IndexOf不是字符串。包含
数组。IndexOf
与数组中的元素精确匹配。在您的例子中,它对字符串的全部内容进行精确匹配。您似乎希望它对每个字符串元素进行部分匹配,最好使用支持DOM的XML解析器。然后,您可以只查找注释节点并将其删除:P@SPlatten:如果是XML,则XML解析器可以解析它。如果没有帮助,可能无法完全理解它,但XML解析器不会被格式良好的XML阻塞,句号。@Rufus,仔细看看,原始版本是将数据读入字符串数组而不是单个字符串。是的,我理解…我是在提示您实际回答问题,以便其他人可以从中学习-例如,描述问题是什么以及您是如何解决的。:)很明显,这个问题不是因为我对指数的误解吗