简体   繁体   中英

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.

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. 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". 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. 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") .

However, given what you are trying to do, a better alternative might be the String.StartsWith method.

It is quite possible that Keyword.Length < "economics". For example, consider Keyword as "ECO". Since, length of ECO is less than that of economics it will throw you 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.

Your keyword comparison also starts with 0 index, is it possible that keyword is in the middle of Title? Example: Course_Economics. Do you need to consider that? Also, do you need to check for string CASE?

One possible condition satisfying above conditions could be below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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