Here are my most commonly used VB expressions for handling date, string, and array/collection values. Will add more to these as I go along. UIPath activities essentially use VB.NET expressions so their API browser is extremely helpful in learning functions and syntax.
.NET API Browser:
https://docs.microsoft.com/en-us/dotnet/api/?view=netframework-4.7.2
String
How to print new line on Message Box
Use vbCrLf
"Hello" + vbCrLf + "World"
How to concatenate string and variable values
Use + operator
"My name is " + var_name
How to split string value based on delimiter
Use the Split method to split a line of text into array values based on a delimiter
Split("apple~bear~cat~dog","~") 'returns an array with 4 values
To access them, you can loop through the array or for smaller and more defined formats you can simply retrieve the value based on its index number.
Split("apple~bear~cat~dog","~")(0) 'returns "apple"
Split("apple~bear~cat~dog","~")(1) 'returns "bear"
Split("apple~bear~cat~dog","~")(2) 'returns "cat"
Split("apple~bear~cat~dog","~")(3) 'returns "dog"
How to combine all array values into a string
Use the Join method
var stringArray = {"Apple","Bear","Cat"}
String.Join("/",stringArray) 'returns "Apple/Bear/Cat"
How to trim leading and trailing spaces from a text
Use the Trim method
Trim(" This is a sentence with lots of spaces. ")
Note that Trim() removes only the spaces at the start and end, not the inside of the string.
How to replace certain characters in a string
Use the Replace method
"apple~bear~cat~dog".Replace("~","!") 'returns "apple!bear!cat!dog"
Date/Time
How to format date value
Use ToString method
Now.Date.ToString("MMMM yyyy") 'returns "January 2018"
For other custom date and time formats:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
How to convert string value to DateTime format
Use Convert class
Convert.ToDateTime("January 31, 2019")
For more details about the Convert class:
https://docs.microsoft.com/en-us/dotnet/api/system.convert?view=netframework-4.7.2
How to get first day of the month
Use the Date function and just format the day value as 1
Now.Date.ToString("MMMM 1, yyyy") 'returns "January 1, 2019"
How to get last day of the month
Use AddMonths method to get the first day of the next month, then subtract 1 day using the AddDays method.
Convert.ToDateTime(Now.Date.ToString("MMMM 1, yyyy")).AddMonths(1).AddDays(-1).ToString("MMMM d, yyyy") 'returns "January 31, 2019" (relative to date posted)
Note that you have to convert the first day of the month back to DateTime in order for the AddDays and AddMonths method to work.
Array/Collections
How to get all files in a folder
Use Directory.GetFiles method
Directory.GetFiles("C:\Data\UIPath","*.*",SearchOption.AllDirectories)
returns a String array of all the files inside the UIPath\ folder, including its subfolders, that matches the search pattern indicated in the 2nd argument.
“*.*” basically takes all types of files. “*.pdf ” will get only files with the .pdf extension.
SearchOption.AllDirectories searches even the subdirectories under UIpath\
Because this method returns a String array, use a For loop to run through the collection.