In a list of objects, add an 'and' before the last one on the list

Happy Tuesday,

Per row, the bot outputs in string format lists of animals, ex:

There are 2 cows, 1 horse, 1 pig, 3 ducks.
There are 1 chicken, 4 horses, 3 cows, 2 pigs.

How to add an ‘and’ before the last # in the string?
For example, it should be … and 3 ducks. … and 2 pigs in examples above.
Thank you, as always.
Regards,

@PPIM_RPA

String.join(“and”,arrayvarable)

Gives you the expected output

The list is in string format and i want the ‘and’ just before the last number only

Hi @PPIM_RPA ,

Could you provide the expression you are currently using that gives out the output in the above format ?

Could also perform something in the below manner :

String.Join(", ",yourListVar.SkipLast(1).append("and "+yourListVar.Last).ToArray)

image

You helped me with this: String.Join(“, “,dt_CSV.AsEnumerable.Where(Function(x) {“Endangered”, “Threatened”}.Contains(x(“ESA Listing Status”).ToString)).GroupBy(Function(x)x(“Lead Office”).ToString).Select(Function(x)x.Count.ToString + " " +x.First.Item(“Lead Office”).ToString + " (” +String.Join(”, ",x.Select(Function(y)y(“Common Name”).ToString).ToArray)+ “)”))

Just want to add an ‘and’ before the last number if possible for grammatical sake

System.Text.RegularExpressions.Regex.Replace("one, two, three",",(?=[^,]+$)",", and")

image

Thank you. The numbers are written out like 1, 3, or 2, and change every time. But I was wondering if Regex.Replace could do this too

Yes mine was an example with a static string but you can just replace that with a variable.

Note to self: Read the friendly manual!
Used Regex Replace to find last comma: “,(?=[^,]*$)” and replaced with “, and”
This solved the issue

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