Issues with Assigning a Substring

Hi all,

I’m trying to extract the numbers from a string such as this;

Smith, Shannon (123456789)

These will be dynamic, but I’ve also practiced using static values. This is all inside a for each loop, iterating through a string.

So far I’m using an activity to assign a value for the index of the first bracket;

Assign
index ← item.IndexOf(“(”)

Using the example above, index is now 15

I want to substring from the bracket to the end minus 2 (there’s also a space), so I have an activity saying

item = item.Substring(index, item.Length-2);

This crashes with the error Index and length must refer to a location within the string with a Parameter name length

Looking at the variables, the item length is 27, so I’m unsure why index (15) and item.Length-2 (25) doesn’t work.

Also I’ve tried, for testing purposes, so create a new variable called itemTest. I have;

Assign
itemTest = item.Substring(0,5)

Once this runs, itemTest completely disappears from my variables table.

Does anyone have any idea of what I’m doing wrong? All I want is to take this string;

Smith, Shannon (123456789)

and return

123456789

Hi @gfr2104 ,

Try this.

Split(Split(“Smith, Shannon (123456789)”,“(”)(1),“)”)(0).ToString

image

You can use Split function to achieve that. No need of finding index.

Hi @gfr2104

Try This : System.Text.RegularExpressions.Regex.Match(var name,“[0-9]+”).Value

Hope this might help you :slightly_smiling_face:

Hi Adyanthaya,

I’ve tried as you suggested, using the variable “item” from my for each loop rather than the static string, but it doesn’t seem to change anything about the string. Is it perhaps that I’m trying to assign it to itself? Right now the workflow is

BEFORE
item: Smith, Shannon (123456789)

ASSIGN
item = Split(Split(item,“(”)(1),“)”)(0).ToString

RESULT
item: Smith, Shannon (123456789)

Hi @gfr2104

Parameter passed to the Substring method is wrong because of that you are getting this error.

Give a try to this.

“Smith, Shannon (123456789)”.Substring(16,9)
image

"Smith, Shannon (123456789)".Substring("Smith, Shannon (123456789)".IndexOf("(")+1,"Smith, Shannon (123456789)".Length-16-1)

image

image

Second parameter which you have passed to the substring method is the index of the last character of substring you required. Instead of this pass the length of substring required.

Hope this helps

1 Like

Hello @gfr2104
Try Regular expressions.

System.Text.RegularExpressions.Regex.Match(YourString,"(?<=\S)\d+(?=\S)").Tostring.Trim

1 Like

You’re spot on that this was the mistake I was making, however when I assign the result to the original string, nothing happens. And when I assign it to a new results string, the new variable disappears.

Upon looking into my initial string variable, after running that line I receive the following in the length section (screenshot as it’s running on a remote desktop)

Hi @gfr2104

Check this.

Brilliant, this words as expected, thanks (although the numbers will always be the same length so I just went with your original note that I didnt need to use the length.

One question I have; I said last time it was wrong because my result variable disappeared from the variables box. Now, using the write line, I can see it definitely stores the variable I want. Do you know why it’s disappearing from the variables box? I’d have just assumed it was null had I not printed it out.

Not sure why variable is disappearing in your case. Can u share screen shot of your workflow and variable panel.

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