Problems with Substring

I have a problem with using substring.

I need to fill up a field which allows only 100 characteristics, let’s say this variable is called MyText.

I have gotten this MyText from another website A and created an excel table, from which I fill up the information to another website B. However, some of the information MyText gotten from this website A is over 100 characters and B field allows only 100 characters at maximum. I’ve been using two assigns, first will change the text from website A into a right format:

MyText = MyText.Replace("MyText: ", “”).ToString

and the another is
MyText = MyText.Substring(0, 99)
And is supposed to cut this text in length of 100 characters.

However, it won’t work out. This is the error message I get:
Assign: Index and length must refer to a location within the string.
Parameter name: length

I feel like I’ve tried everything: does anybody know how to solve this problem?

I guess before MyText = MyText.Substring(0, 99) you should check the length of string and if it is greater than 100 characters then only this assignment statement should execute.

Hi, thank you for the reply.

Could you please tell me with which command I need to do this?

if your text isn’t long enough, there will be exception(out of index)
Use If to check the length of your text

if(MyText.Length >= 100),
then MyText = MyText.Substring(0, 99)
else you don’t need to Substring

1 Like


1 Like

Thank you so much for both of you, now it works!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.