Merge every second line of string to previous one

Hello I need merge every second line from string with the previous line. I.e my string looks like this
aa bb SRC 8
aabbccc xxxxx.
ab bc SRC 8
aaabb xxx.
ba bbc $AT 8
ccbbaa xxxxxx.

And I need it to change to this
aa bb SRC 8 aabbccc xxxxx.
ab bc SRC 8 aaabb xxx.
ba bbc $AT 8 ccbbaa xxxxxx.
and then put it into datatable.
Thanks for help

@Katerina_Chabova

  1. Read the input string:
  • Use the Read Text File activity to read the input string from a file or use any other method to obtain the input string.
  1. Split the string by new line characters:
  • Use the Assign activity to split the input string into an array of lines:

mathematicaCopy code

inputLinesArray = inputString.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  1. Merge every second line with the previous line using LINQ query:
  • Use the Assign activity with a LINQ query to merge every second line with the previous line:

vbnetCopy code

mergedLinesArray = inputLinesArray.Select(Function(line, index) If(index Mod 2 = 0 AndAlso index + 1 < inputLinesArray.Length, line &amp; " " &amp; inputLinesArray(index + 1), line)).Where(Function(x) x IsNot Nothing).ToArray()
  1. Create a DataTable and add the merged lines to it:
  • Use the Build Data Table activity to create a DataTable with the desired columns.
  • Use a For Each activity to iterate through the mergedLinesArray and add each merged line as a row to the DataTable using the Add Data Row activity.

@Katerina_Chabova,

You can split the string and use While loop to accomplish this.

Sample:

Split.xaml (15.5 KB)

Not sure about your Datatable structure but that’s simple once you have this covered.

Thanks,
Ashok :slight_smile:

HI,

How about the following expression?

System.Text.RegularExpressions.Regex.Replace(yourString,"([^\r\n]*)\r?\n([^\r\n]*(?:\r?\n|$))","$1$2")

Regards,

2 Likes

Hi @Katerina_Chabova


Regex_Practice.xaml (22.3 KB)

Regards

Don’t know why but I can’t split string into array. The whole table is one array. I have a lot of spaces at the end of each line, maybe that is the reason?

@Katerina_Chabova

You can use this as well

string.Join(vbLf,stroutput.Split(vbLf,StringSplitOptions.RemoveEmptyEntries).Chunk(2).ToArray.select(Function(a) string.join(" ",a)).toarray)
1 Like

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