Need to avoid index out of range

Hi everyone,

My required is

subject of the mail is “Externals appointments held - Kristan Tay”

strName = Strings.split(subject.toString,“-”)(1) — is what i am using to extract

but in some cases when subject is like "Externals appointments held "
It throws out of range , please could u provide a solution
@ashwin.ashok @Palaniyappan could u please help

Thanks in advance

1 Like

Hi

Fine then

Have their simple expression in your assign activity

stroutput = If(Strinput.ToString.Contains(“-“), Split(Strinput.ToString, “-“)(1).ToString, String.Empty)

So if it contains - then it splits else give empty string

Cheers @kavya.s16

Hi @kavya.s16

You have a few options.

Option 1: Try Catch
You could use a try catch to catch this exception when you are using the result? With the exception type being: IndexOutOfRangeException

IndexOutOfRangeException - Occurs when the index of an object is out of the limits of the collection.
How to use Try Catch - Help - UiPath Community Forum
Try Catch (uipath.com)

Option 2: Count results using If Statement (Combine with Regex or String Manipulation)
Checkout this Regex Pattern
Then count the number of results. If results = 0

Condition: INSERT_REGEX_Variable.Count = 1
THEN: Continue.
Else: Do nothing.

Please let me know if I have misunderstood.

Cheers

Steve

Hi,

FYI, another solution:

The following expression will also work.

System.Text.RegularExpressions.Regex.Match(subject.ToString,"(?<=-)[^-]+").Value

Regards,

Hi @kavya.s16

Another solution

Try this expression

 System.Text.RegularExpressions.Regex.Match(subject.ToString,"(?<=-\s)(\S.+)").Tosting.Trim

image

Regards
Gokul

Thanku @Palaniyappan it worked

Glad it got resolved @kavya.s16

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