tulsi01 says:
Hello,
Iam trying to split the string based on length, when the string is greater than 255 charcters it is storing first 255 in comment1 but rest of them are not storing in comment2 .am getting an exception .
Below is the code !
if (Comment.Length >= 0 && Comment.Length <= 254)
{ COMMENT1 = txtComment.ToString(); } else if (Comment.Length >= 255 && Comment.Length <= 510) { COMMENT1 = txtComment.Substring(0, length); COMMENT2 = txtComment.Substring(255, length);
}
Thanks
Tulsi
It happens because your logic is wrong. Here for first comment1 you just need the text till 255 and for comment2 you need text after 255 lengths. So the code you used it fails because of that you are getting error. Substring first takes the starting position from where it will start to get string and total length after starting length.
Check below link how to use Substring so you will get idea where you are wrong
https://msdn.microsoft.com/en-us/library/windows/apps/aka44szs%28v=vs.105%29.aspx
Also change your code accordingly I just change it to solve the error by changing the logic for first comment length and for second comment length.
else if (Comment.Length >= 255 && Comment.Length <= 510)
{
int firstcommentlength = 255;
int secondcommentlength = length - firstcommentlength;
COMMENT1 = txtComment.Text.Substring(0, firstcommentlength);
COMMENT2 = txtComment.Text.Substring(255, secondcommentlength);
}