1

Other questions (MSBUILD Splitting text file into lines) mention implementation-specific alternatives, but none seem to directly address how to split a simple string property into an item group based on endlines.

How can you do this? Attempts that didn't work:

  • <ItemGroup> <SplitLines Include="$(SourceString.Split('\r\n'))" /> </ItemGroup>: (splits on 'r' or 'n')
  • <ItemGroup> <SplitLines Include="$(SourceString.Split('%0A%0D'))" /> </ItemGroup>: (doesn't split at all)

In case you're curious: SourceString is the output of an Exec command that needs splitting, so ReadLinesFromFile isn't an option. It can't output to an intermediary file because file systems are slow and this needs to be used by build processes that care about file operations.

Community
  • 1
  • 1
Josh
  • 1,997
  • 13
  • 21

1 Answers1

2

Using property functions is the way to go and you can search for sulutions using e.g. 'C# split string lines' in your search engine of choice, then translate the answer. This comes up with this SO question and the Regex.Split method is the easiest to implement:

<ItemGroup>
  <SplitLines Include="$([System.Text.RegularExpressions.Regex]::Split(`$(SourceString)`, `\r\n|\r|\n`))" />
</ItemGroup>
Community
  • 1
  • 1
stijn
  • 34,664
  • 13
  • 111
  • 163