Get different names from string

Hi Community,

We are fetching details from a Jira ticket using an API.

One of the fields in the ticket contains usernames entered manually by users in different formats, such as:

  • Example 1: John,David,Smith

  • Example 2: John ,David;Smith

  • Example 3: John;David; Smith

Currently, I am trimming spaces, and for cases where values are comma-separated (Example 1), I am able to split them into an array using comma delimiter.

However, since users are entering values using both commas and semicolons, I need a way to handle all formats and retrieve the usernames correctly as an array.

Any suggestions would be helpful.

Hi @Mohammed_Shahid01

Use the below expression:

arr_Users = str_Users.Split({","c, ";"c}, StringSplitOptions.RemoveEmptyEntries).Select(Function(x) x.Trim()).ToArray()

  1. Split({","c, ";"c}) โ†’ Splits the string using both comma , and semicolon ;
  2. StringSplitOptions.RemoveEmptyEntries โ†’ Removes blank values if any
  3. Select(Function(x) x.Trim()) โ†’ Removes extra spaces from each username
  4. ToArray() โ†’ Converts the result into an array

This will work for all below formats:

  • John,David,Smith
  • John ,David;Smith
  • John;David; Smith

Thanks!

Hi @Mohammed_Shahid01

You can try the below expression

userNamesArray = System.Text.RegularExpressions.Regex.Split(userNames.Trim, โ€œ\s*[,;]\s*โ€)

Check the below thread for your reference in a different approach

Hope it helps!!

Have your text in a string variable say strText and an array variable where you want to store the output and do the below operation.

strText = "John ,David;Smith"
arrOutput = strText.Split(new char[] { ',', ';'}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray()

Or

arrOutput = strText.Split({","c, ";"c}, StringSplitOptions.RemoveEmptyEntries).Select(Function(x) x.Trim()).ToArray()

You can more delimiter characters as per your need and you will have your expected result.

Hi @Mohammed_Shahid01 ,

Instead of depending on the separator, focus on the names, what if tomorrow delimiter got chnaged, for that use regex this will give you the array names as per your input,

System.Text.RegularExpressions.Regex.Split(strVar, โ€œ[^A-Za-z0-9._]+โ€)

Happy Automation
YK

Hi @Mohammed_Shahid01

Just replace all semicolons with commas, then split:

cleanString = input.Replace(โ€œ;โ€, โ€œ,โ€)
arr = cleanString.Split(","c).Select(Function(x) x.Trim).ToArray