Understanding Substring & LastIndexOf() working

Input Message: IE doesn’t have a native XPath engine for finding elements. So to be able to find elements by XPath in IE,we have to use a JavaScript XPath query engine.

Required Output: XPath query engine.

What I am trying to do:
message.Split(","c)(1).ToString.Substring(message.LastIndexOf(“XPath”))

Can anyone please help ?

Error faced: 20.4.1

Source: Assign

Message: startIndex cannot be larger than length of string.
Parameter name: startIndex

Exception Type: System.ArgumentOutOfRangeException

RemoteException wrapping System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Parameter name: startIndex
at System.String.Substring(Int32 startIndex, Int32 length)
at lambda_method(Closure , ActivityContext )
at Microsoft.VisualBasic.Activities.VisualBasicValue1.Execute(CodeActivityContext context) at System.Activities.CodeActivity1.InternalExecuteInResolutionContext(CodeActivityContext context)
at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity1 expressionActivity) at System.Activities.InArgument1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance activityInstance, ActivityExecutor executor)
at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate) at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

hi @Akash_Malpure,

Try this,
message.Substring(message.LastIndexOf("XPath"))

Thanks ! This worked

Hi @samir,

It is very interesting that in this case:
message.Split(".“c)(0).ToString.Substring(message.LastIndexOf(“author”)) , the above error is not encountered while in this case message.Split(”,"c)(1).ToString.Substring(message.LastIndexOf(“XPath”)) the error is encountered.

Can you please throw some light as to why is the first case working and the 2nd case is throwing error ?

Sure @Akash_Malpure,
As we know lastIndexOf() method returns the position of the last occurrence of a specified value in a string.
So in your case when you use

message.Split(","c)(1).ToString

by this your’re eliminating string behind , (Comma)
And then you’ve applied this

.Substring(message.LastIndexOf("XPath"))

by this the problem is, you’re using substring on half string (string you’ve splitted) and getting LastIndex of “XPath” from message string (Full String)

When you split the string, it’s length of the string automatically decreases, hence index get changed, so while using substring on that splitted string, getting index from original string make no use. It gives you greater value as index and that’s why following error occurred

So, to handle this,

  1. to substring you should get index from that splitted sting only like this,
    message.Split(","c)(1).ToString.Substring(message.Split(","c)(1).ToString.LastIndexOf("XPath"))

OR

  1. I had suggested this,

because I’ve noticed as using LastIndex there’s no use of splitting it, So you can use as your requirement.

I hope you understand my explanation :slight_smile:

1 Like

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