Click here to Skip to main content
15,891,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I need to learn substrings etc and I'm a novice in ASP.net and I have this code
C#
protected void Page_Load(object sender, EventArgs e)
  {
      if (!Page.IsPostBack)
      {
          TextBox1.Text = "RHR 100";
      }


  }
  protected void Button1_Click(object sender, EventArgs e)
  {
      string myString = TextBox1.Text.ToString();
      string substring = myString.Substring(0, 3);
      string substring2 = myString.Substring(3, 6);

      Label1.Text = "Substring[0,3]: " + substring;
      Label2.Text = "Substring[3,6]: " + substring2;


  }


and when I click on the button. Label 1 should be RHR and Label 2 should be 100 but it keeps having this error
SQL
Index and length must refer to a location within the string.
Parameter name: length


Why is this so? Thank you! :)
Posted
Updated 6-May-21 6:05am
Comments
[no name] 15-Mar-13 10:49am    
You are asking the SubString for a string starting at 3 with a length of 6 and your string is not that long. You want SubString(3, 3)

The second parameter of String.Substring is length. I changed the 6 to a 3.

C#
string myString = TextBox1.Text.ToString();
string substring = myString.Substring(0, 3);
string substring2 = myString.Substring(3, 3);



See String.Substring Method (Int32, Int32)[^]
 
Share this answer
 
v2
Comments
BeastMode10 15-Mar-13 11:16am    
Thank you! :)
This is incorrect:
C#
string substring2 = myString.Substring(3, 6);

First parameter: Starting position
Second parameter: No of characters to extract (i.e. it has to be such that it does not goes out of string's length overall.) Hope you get the logic.

Thus, it should be:
C#
string substring2 = myString.Substring(3, 3);

Refer: MSDN: String.Substring Method (Int32, Int32)[^]
 
Share this answer
 
Comments
BeastMode10 15-Mar-13 11:15am    
Thank you so much!
Sandeep Mewara 15-Mar-13 11:49am    
Welcome.
// what if we can't know the length of the string...

string substring2 = SafeSubstring(myString, 3, 6);

public string SafeSubstring(string sSafeString, int iStart, int iLength)
{
// Replacement for Visual Basic function Mid().
// Unlike .Substring function, Mid() doesn't make error when start character
// position or end character is outside of string. If so, it returns 'string.Empty'.
// IMPORTANT: SafeSubstring() starts from character 0, Mid() from 1.

string sSubstring = string.Empty;

int iStringLen = sSafeString.Length;
int iPointer = iStart;

if (iStringLen > 0 & iStart >= 0 & iStart < iStringLen & iLength > 0)
{
while (iPointer < iStart + iLength & iPointer < iStringLen)
{
sSubstring += sSafeString.Substring(iPointer, 1);
iPointer += 1;
}
}
return sSubstring;
}
 
Share this answer
 
v2
Comments
CHill60 6-May-21 12:09pm    
Rather than resurrecting such an old post with this it might have been better to post it as a Tip. You would of course have to add some commentary and format your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900