How to split the string based on string index as even and odd

Hi all I have a string ‘ABCDE’

so I want the output for even index is like shown below

A
C
E

for odd index need the output as shown below

B
D

Kindly help me to get the logic for this problem

Thanks in advance

@chandolusathi.kumar

Try this

Evenarray = Str.Where(function(x,i) (i mod 2) =0).ToArray

oddarray = Str.Where(function(x,i) not (i mod 2) =0).ToArray

Cheers

Hi,

FYI, another approach using regex.
If you need char array, the following will work

System.Text.RegularExpressions.Regex.Replace(yourString,"(?s)(.).?","$1").ToArray()

System.Text.RegularExpressions.Regex.Replace(yourString,"(?s).(.?)","$1").ToArray()

Regards,

Hi @chandolusathi.kumar

Drag and drop a “Matches” activity onto your UiPath workflow.In the properties panel of the “Matches” activity, set the following values:

  • Input: 'ABCDE' (or the variable holding your input string)
  • Pattern: "(?<=.)(.)" for even index or "(?<=.)(.)(?=.|$)" for odd index
  • Result: Create a new variable, let’s say evenMatches for even index or oddMatches for odd index.
  • RegexOptions: None
    Add a “For Each” activity to iterate over the matches.
  • Set the “TypeArgument” property of the “For Each” activity to System.Text.RegularExpressions.Match.
  • Set the “Values” property to evenMatches or oddMatches depending on the desired index.

Thanks!!

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