How to check if a string contains substring?

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

user1679
Posts: 1088
Joined: Fri, 20. Jul 18, 23:20

How to check if a string contains substring?

Post by user1679 »

I was trying to use the XML function contains to search for a substring but my script errors with "operator expected"
<do_if value="not contains($completedname, $factionprefix)">
The "operator expected" error in the log points to the ( after "contains". I didn't see anything in the x4 documentaton about strings
other than formatting. Does the x4 API even support this type of action?
kuertee
EGOSOFT
EGOSOFT
Posts: 818
Joined: Sun, 14. Dec 03, 13:05
x4

Re: How to check if a string contains substring?

Post by kuertee »

unfortunately, there's no such function.
but there's a hack that can be used: substitute_text the text you want to find with an arbitrary string.
then test that resulting string.
e.g.:

Code: Select all

<set_value name="$completedname_orig" exact="$completedname" />
<substitute_text text="$completedname">
    <replace string="$factionprefix" with="'EXISTS'" />
</substitute_text>
<do_if value="$completedname_orig != $completedname">
    <!-- if $completedname changed then the substitute was successful and $factionprefix exists -->
    <debug_text text="'$factionprefix: ' + $factionprefix + ' EXISTS!'" />
</do_if>
credit: unitrader from discord
user1679
Posts: 1088
Joined: Fri, 20. Jul 18, 23:20

Re: How to check if a string contains substring?

Post by user1679 »

kuertee wrote: Sun, 29. May 22, 14:31 unfortunately, there's no such function.
but there's a hack that can be used: substitute_text the text you want to find with an arbitrary string.
then test that resulting string.
e.g.:

Code: Select all

<set_value name="$completedname_orig" exact="$completedname" />
<substitute_text text="$completedname">
    <replace string="$factionprefix" with="'EXISTS'" />
</substitute_text>
<do_if value="$completedname_orig != $completedname">
    <!-- if $completedname changed then the substitute was successful and $factionprefix exists -->
    <debug_text text="'$factionprefix: ' + $factionprefix + ' EXISTS!'" />
</do_if>
credit: unitrader from discord
Thanks a lot. I was working on the PlayerFactionTag mod (which seems abandoned since 2019) that adds your tag to ships you build because I found that on login it was adding the tag even if it existed.

Return to “X4: Foundations - Scripts and Modding”