Assign: Index was outside the bounds of the array error 2

Assign: Index was outside the bounds of the array.



what do you need to do?

Hi @Soudios ,

Were you trying to get a portion of the string?
If so, then using the Substring Method should help you out →


Or if you were trying to retrieve a single character from it, then there is a chance that the string is less that 2 characters long, which is why when you substract two from it you receive a negative index which leads to this error.

If not, then you could try playing around with the values first in Immediate Panel before introducing it into your actual code.

Kind Regards,
Ashwin A.K

The error means you are trying to reference an index in an array (or other enumerable) that doesn’t exist. For example…

myArray = {“test”,“test 2”,“test 3”}

myArray(3) will cause the error you’re seeing because there is no element with index 3 (4th element, arrays are 0 based).

Hello @Soudios,

You need to use substring method: myvar = secu2.substring(0, secu2.length -2).

1 Like

secu2 is a string, not an array. arrayVarName(number) is how you reference the elements of an array.

i need to count the number of the caracter on secu2 and after that i need to substring the two first caractere

Hi @Soudios
if you want to check length first and then you need to remove first 2 characters

if secu2.length>0

then secu2.substring(0, secu2.length -2)


Hi @Soudios ,

We can also do this inline using an Assign Activity :

secu2 = if(secu2.Length>2,secu2.Substring(0,2),secu2)

If you do want to Throw out an Error if the secu2 variable has less then 2 characters, then you could opt for if Activity instead of the inline if.

what i need is count the number of caractere first and then substring after because the rule is different depending on the number of characters

@Soudios ,

We can count the Number of Characters and assign it to an Integer variable using the Expression below :

characterCount = secu2.Length

And if we have multiple rules based on the number of Characters, we might need to use a Switch Activity with Type Argument as Integer.

Then Based on the Number of Character you could perform the operation required :

its ok for
characterCount = secu2.Length

but i dont really understand the activity switch

@Soudios ,

The Switch Activity is one of Conditional Statements that we can use. The If Activity already familiar, provides two Branches if the Condition is True or False. Where as the Switch Provides us with N branches and selects the branch based on the Value Matched.

Take a Look at the documentation below :

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