Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii ,

I am trying to remove first 2 char from my string ..

But its throwing Index and count must refer to a location within the string. error

lblDataForYear.Text = expYear.ToString() + "/" + (expYear + 1).ToString().Remove(0,2);
Posted
Comments
[no name] 27-Nov-14 8:07am    
What is the expYear value (I/P)?

what is you expected O/p ?
Torakami 27-Nov-14 8:17am    
its a integer value .. 2014

the way i want to print is 2014/15
[no name] 27-Nov-14 8:26am    
Your syntax is correct, however put a breakpoint and check the expYear value.

Hi,

Assuming that expYear is numeric type and has 4 digits try this way:


C#
lblDataForYear.Text = string.Format("{0}/{1}", expYear, (expYear + 1).ToString().Substring(2, 2));


Cheers!
 
Share this answer
 
v4
Comments
Torakami 27-Nov-14 8:19am    
Thanks brother ... you rock ... it worked as expected ... cool stuff
Marcin Kozub 27-Nov-14 8:20am    
Happy to help you :)
It is clear from the exception message that the actual error is coming from the .Remove() method you're calling on the String. The parameters that you're passing must be valid, I mean that the total number of the characters in your string must be enough for this function to work on. As far as I know the expYear is either an empty string or it contains only one character. I have an example of this problem, where this error message is shown because the string contained only one character. https://dotnetfiddle.net/DTspxj[^]

C#
// variable
int year = 1;
// write the string without first two characters
Console.WriteLine(year.ToString().Remove(0, 2));
// but error! Index and count must refer to a location within the string.


However this second code, is working even if the variable contains two characters, but I added 2 more to make it 1995, and it gave me 95.

C#
// variable
int year = 1995;
// write the string without first two characters
Console.WriteLine(year.ToString().Remove(0, 2));


So what might you do to minimize this, is to check if the characters are enough before you proceed as,

C#
if(expYear.Length > 1) {
   // if contains more than 1 character, then
   lblDataForYear.Text = expYear.ToString() + "/" + 
                         (expYear + 1).ToString().Remove(0,2);
}
 
Share this answer
 
Comments
Torakami 28-Nov-14 1:26am    
Strange , But i am very much sure i am not passing single character value ././ I was passing complete 2013 as a expyear.
Afzaal Ahmad Zeeshan 28-Nov-14 2:14am    
Well, such things happen. It is always good to write all the precautionary measures for the code block that is sensitive to lengths and the data you're passing. :-) Use if else blocks to determine the data before you pass it and so on.
Torakami 28-Nov-14 4:36am    
yeh man ... thanks for the tip ...

Marcin Kozub's solution solved my problem .. thanks again

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