How to check if a string contains substring?

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

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

Post Reply
user1679
Posts: 789
Joined: Fri, 20. Jul 18, 23:20

How to check if a string contains substring?

Post by user1679 » Sun, 29. May 22, 07:49

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: 789
Joined: Sun, 14. Dec 03, 13:05
x4

Re: How to check if a string contains substring?

Post by kuertee » 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
Mods: RPG: Reputations and Professions, Social Standings and Citizenships, Crime has Consequences, Alternatives to Death. Missions/NPCs: Emergent Missions, NPC Reactions, Mod Parts Trader, High-sec Rooms are Locked, Hacking Outcomes, More Generic Missions, Waypoint Fields for Deployment. Others: Auto-cam, Friendly Fire Tweaks, Teleport From Transporter Room, Wear and Tear. QoL: Trade Analytics, Loot Mining, Ship Scanner, Signal Leak Hunter, Station Scanner, Surface Element Targeting, etc.

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

Re: How to check if a string contains substring?

Post by user1679 » Mon, 30. May 22, 02:32

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.

Post Reply

Return to “X4: Foundations - Scripts and Modding”