Convert part of a string into int32

Hello all,

So I have a string variable, that is dynamic, with some numbers and I want to extract those numbers.

Some examples:
- var1 = “19 de Agosto de 2020” (19 and 2020)
- var2 = “.5 de Janeiro de 2020,” (5 and 2020)
- var3 = “9 de Dezembro de .2019” (9 and 2019)

Some thoughts?

Thank you :slight_smile:
Cátia

I would recommend using regex to parse your string variables and pull out all the individual numbers, then store each match you find in a list or array.

You state that you want to convert into an integer, so I will use a regex that doesn’t account for decimals at all. Here is how I would do it:

Assign MyListOfNumbers = new list(of int32)

Use the ‘matches’ activity with the following inputs/outputs:
Input: var1 (or whatever string you want to pull out the numbers from)
Pattern: “\b\d+\b”
RegexOption: IgnoreCase, Compiled
Result: Var1NumMatches

Use a for each activity and change the TypeArgument to System.Text.RegularExpressions.Match
For each num in Var1NumMatches
‘Add to collection activity’ with the following inputs/outputs:
Collection: MyListOfNumbers
Item: Cint(num.value)
TypeArgument: int32

Now you have a list called MyListOfNumbers that contains all of the individual integers listed in your original string.

EDIT: You’ll likely want to add some logic in there in case your string doesn’t contain any numbers. Something as simple as if Var1NumMatches.count = 0, then (your code to handle it here) would suffice

1 Like