Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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/2/joomla/2.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#_.net_Asp.net Mvc 4 - Fatal编程技术网

C# 返回带有注释标记的字符串的一部分

C# 返回带有注释标记的字符串的一部分,c#,.net,asp.net-mvc-4,C#,.net,Asp.net Mvc 4,我正在寻找最有效的方法来获取字符串的一部分,并使用HTML注释作为起点/终点 例如,我想使用 <!-- START: Variable --> stuff goes here <!-- END: Variable --> 字符串应该是注释之间的内容。我已经成为了一个助手,但对.net来说不是很好 @helper readPattern(string filepath, string patternSection = null) { TextReader t

我正在寻找最有效的方法来获取字符串的一部分,并使用HTML注释作为起点/终点

例如,我想使用

<!-- START: Variable -->

stuff goes here

<!-- END: Variable -->
字符串应该是注释之间的内容。我已经成为了一个助手,但对.net来说不是很好

@helper readPattern(string filepath, string patternSection = null) {

    TextReader tr = new StreamReader(filepath);
    string SplitBy = "<!-- START: "+patternSection+" -->";

    string fullLog = tr.ReadToEnd();

    fullLog.Trim().StartsWith("<!-- " + patternSection+ " -->");

    //string[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.RemoveEmptyEntries);

     <div class="@patternSection">@fullLog</div>
}

我不需要拆分它,我只需要从我通过该助手传递的注释中输出或修剪它。

您需要标记开始和结束,然后抓住中间的内容。介于“开始+开始字符串长度”和“结束-开始+开始字符串长度”之间


顺便说一下,您还需要关闭/处理您的阅读器。如果我的假设是正确的,并且这个助手将反复使用相同的文件路径,那么缓存文件的内容将对您有好处,而不是每次调用不同的patternSection时都反复读取它。对于其他静态文件来说,这将导致大量文件打开和关闭。

您可以使用子字符串2次如何缓存它?你会怎么关闭它,对不起,我是新手。另外,我在数据字符串上得到一个错误,Splitby似乎不在contextI中,我输入了一个Splitby而不是Splitby。我提供的建议是我能得到的最好的,除了完全为您编写代码之外,对不起。因此,我们期望对手头的任务有最低限度的了解。由于您的问题主要是获取注释块之间的数据,请尝试让我的代码正常工作,如果它满足您的需要,请将其标记为答案。然后,如果你有其他问题,点击黑板,看看是否有答案给你;如果没有,请随意问另一个问题。祝你好运
@helper readPattern(string filepath, string patternSection = null) {

    TextReader tr = new StreamReader(filepath);
    String SplitBy = "<!-- START: "+patternSection+" -->";
    String EndBy = "<!-- END: "+patternSection+" -->";

    String fullLog = tr.ReadToEnd();

    int start = fullLog.IndexOf(SplitBy);
    int end = fullLog.IndexOf(EndBy);

    String data = fullLog.Substring(start + SplitBy.Length, end - (start + SplitBy.Length));

    <div class="@patternSection">@data</div>
}