- Home /
Substring Exception
I'm having an issue using Substring, whenever my search input exceeds 5 characters, I keep getting the following Exception:
ArgumentOutOfRangeException: startIndex + length > this.length
Parameter name: length
System.String.Substring (Int32 startIndex, Int32 length)
MyScript.OnGUI () (at Assets/MyScript.cs:757)
Here's what I'm using to call it, anyone got some advice?
searchLength = mySearchStr.Length;
if(Regex.IsMatch(myArray[i].name.Substring(0,searchLength), mySearchStr, RegexOptions.IgnoreCase))
//Yada yada
Answer by Berenger · Mar 19, 2012 at 04:42 PM
I guess myArray[i].name.Length is lower than searchLength. You can fix that with Mathf.Min( (...).name.Length, searchLength ), or other similar ways.
Ahh, yeah, that was indeed the problem.. I didn't think that because I was looping through all of the objects inside myArray that it would have a problem with anything with less characters than the current search string.
Here's what I used in the end, for anyone else having a similar issue:
if(myArray[i].name.Length < searchLength)
continue;
Your answer
![](https://koobas.hobune.stream/wayback/20220613065312im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Substring vs Regex 2 Answers
How to replace a word inside a string with different white space combos? 0 Answers
Allow only numbers in an input field 1 Answer
Creating a card game. Best ways to interpret the various cards? 1 Answer
Replace() with string and Regex not replacing numbers/integers 2 Answers