Bob
Bob

Reputation: 5010

How to check for presence of a string in a property

If I have a property

<PropertyGroup>
  <MyProp>abd;efg;hij;klm</MyProp>
</PropertyGroup>

How do I parse $(MyProp) to check the presence of klm?

Upvotes: 0

Views: 1055

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100811

You can use property functions to invoke the string Contains() method to check for string occurrences. While some other options using the Items (through an Include="$(MyProp)" and checking if an item with the expected identity exists) is also possible, conditions using property functions can be used on any msbuild element, both inside and outside of targets.

Example:

<Project>
  <PropertyGroup>
    <MyProp>abd;efg;hij;klm</MyProp>
  </PropertyGroup>

  <Target Name="Build">
    <Message Importance="high" Text="klm is included!" Condition="$(MyProp.Contains('klm'))" />

    <PropertyGroup>
      <MyProp>;$(MyProp);</MyProp>
    </PropertyGroup>
    <Message Importance="high" Text="exactly klm is included!" Condition="$(MyProp.Contains(';klm;'))" />
  </Target>
</Project>

The second approach - pre- and appending ; and checking for ;klm; - makes sure that the string is matched as a whole in the list can deal with ;aklm.

Upvotes: 2

Related Questions