简体   繁体   English

子串异常

[英]Substring exception

I'm trying to write a basic search, and part of the sort logic compares the captured keyword length against the returned results Substring based on the same length. 我正在尝试编写基本搜索,并且排序逻辑的一部分将捕获的关键字长度与基于相同长度的返回结果子字符串进行比较。 So if the keyword is 9 characters, the logic then searches the first 9 characters of each returned result to find if the keyword matches the Substring. 因此,如果关键字为9个字符,则逻辑将搜索每个返回结果的前9个字符,以查找关键字是否与Substring匹配。

The problem I get is that if I search against the keywords length, it claims that I am out of range on the Substring index of the data I'm comparing the keyword to, even though the only results returned will at the very least contain the captured keyword length and more. 我得到的问题是,如果我针对关键字的长度进行搜索,它声称我在与关键字进行比较的数据的Substring索引上超出了范围,即使返回的唯一结果至少将包含捕获的关键字长度及更多。 My code is below: 我的代码如下:

  @{
string KEYWORDS = !string.IsNullOrEmpty(Request.QueryString["keywords"]) ? Request.QueryString["keywords"] : string.Empty;


var query1 = Query.WhereFullTextContains(KEYWORDS, In.Column("Property_Title", 100), In.Column("MD_Course_UCASCode", 50));
var query2 = Query.WhereFullTextContains(KEYWORDS, In.Column("MD_Course_Departments", 50), In.Column("MD_Course_Departments2", 50));


var search = new NodeFinder().Find(query1);
var search2 = new NodeFinder().Find(query2);
int searchindex = KEYWORDS.Length;

var searchall = search.Union(search2).GroupBy(x => x.Title).Select(y => y.First());


}



  @foreach (ContentNode node in searchall) {

if (node.Title.Substring(0, searchindex) == "economics")
{

<div class="sys_subitem">
                    <div>
                        <h3><a href="@node.Path">@node.Title</a></h3>
                    <div>
                                           <dl>                                               
                                                <dt>UCAS Code:</dt>
                                                <dd>@node.Data.Course_UCASCode</dd>
                                            </dl>
                                        </div>
                    </div>
                  </div>
  }
}

Any ideas what I might be doing wrong? 有什么想法我可能做错了吗? Help would be appreciated! 帮助将不胜感激!

If you run this code snippet: 如果您运行以下代码段:

"".Substring(0,9)

The output is an ArgumentOutOfRangeException with the message "Index and length must refer to a location within the string.Parameter name: length". 输出为ArgumentOutOfRangeException ,消息为“索引和长度必须引用字符串中的位置。参数名称:长度”。 In fact this will happen any time that you're asking for a slice of the string that is bigger than string you're searching in. 实际上,只要您要求的字符串片段大于要搜索的字符串,这种情况就会发生。

So what's probably happening is node.Title is empty or at least smaller than the keyword you're searching for. 因此,可能发生的是node.Title为空或至少小于您要搜索的关键字。 So, to fix this and use your current approach, you would need to change your logic to (node.Title.Length >= searchindex && node.Title.Substring(0, searchindex) == "economics") . 因此,要解决此问题并使用当前的方法,您需要将逻辑更改为(node.Title.Length >= searchindex && node.Title.Substring(0, searchindex) == "economics")

However, given what you are trying to do, a better alternative might be the String.StartsWith method. 但是,鉴于您要尝试执行的操作,更好的替代方法可能是String.StartsWith方法。

It is quite possible that Keyword.Length < "economics". Keyword.Length <“ economics”很可能。 For example, consider Keyword as "ECO". 例如,将关键字视为“ ECO”。 Since, length of ECO is less than that of economics it will throw you ArgumentOutOfRangeException . 由于ECO的长度小于经济学的长度,因此会抛出ArgumentOutOfRangeException Also, since you are also doing search on Department it is possible that Title is null or empty, hence you should also do a null check. 另外,由于您也在Department上进行搜索,因此Title可能为null或为空,因此也应进行null检查。

Your keyword comparison also starts with 0 index, is it possible that keyword is in the middle of Title? 您的关键字比较也从0索引开始,关键字是否可能在标题中间? Example: Course_Economics. 示例:Course_Economics。 Do you need to consider that? 您需要考虑吗? Also, do you need to check for string CASE? 另外,您是否需要检查字符串CASE?

One possible condition satisfying above conditions could be below: 满足以上条件的一种可能条件如下:

if (!string.IsNullOrEmpty(node.Title) &&
 node.Title.IndexOf("economics", StringComparison.OrdinalIgnoreCase) >= 0) {
     //DO Work
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM