# Breaking up a string in a expression

**URL:** <https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534>\
**Category:** Ignition\
**Created:** [December 6, 2012, 12:21pm UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534 "2012-12-06T12:21:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat.stave](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@pat.stave](https://forum.inductiveautomation.com/u/pat.stave)\
**Post date:** [December 6, 2012, 12:21pm UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534/1 "2012-12-06T12:21:03Z")

</div>

I want to do some formatting of a text string in a expression.  
The string is my current “Window Path”. It is stored in a client tag.

The data stored in the tag would typically be something like:  
“Plant/Production/BioTech/OilFactory/Tricanter”

Based on a user selection (integer) in a IO-field i want to break up this string.  
If the user selects “1” then the expression should return: “Plant/”  
If the user selects “2” then the expression should return: “Plant/Production/”  
If the user selects “3” then the expression should return: “Plant/Production/BioTech/”  
… and so on.

I’m struggling to do this in a expression as it seems i can not use a FOR-loop. Do i need to do this operation in a Jython-script?

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [December 6, 2012, 3:22pm UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534/2 "2012-12-06T15:22:22Z")

</div>

You could do it for a finite number of custom properties that represent each piece, then an expression that concatenates them.

As far as an elegant way - I think it should be possible. I created a custom property of type DataSet and bound it to the following expression:

```auto
split({Root Container.Text Field.text}, '/')
```

That gets you a dataset with rows corresponding to values between your split character.

I then created a string type Custom Property and bound it to the following:

```auto
groupConcat({Root Container.dataset},0, '/')
```

That reconstructs the entire string. I just need to figure out how to reference the first x number of rows of the DataSet. As a possible feature request, either an optional parameter to the split function for the first x occurrences, or a subset function that works on datasets would be perfect. There is likely currently a way to accomplish this that I am missing.

I’ll work on this more later if nobody provides a complete solution.

---

<div class="post-metadata">

**Author:** ![pat.stave](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@pat.stave](https://forum.inductiveautomation.com/u/pat.stave)\
**Post date:** [December 7, 2012, 7:05am UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534/3 "2012-12-07T07:05:53Z")

</div>

Hi Nathan,

I’ve already tried this approach, but as you say the problem is that there doesen’t seem to be easy to return only x number of rows from a dataset.

What I’m actually trying to accomplish is to “dynamize” the alarm path property of the alarm summary based on the displayed window path.  
We have several departments in house, and the operator should only be notifyed of alarms from his department (or really the department of the window he is viewing).

For example the alarm path when viewing “Plant/Production/BioTech/OilFactory/Tricanter” should be “Plant/Production/BioTech/” as this is the Biotechnical department.

However, if specifically set as a user parameter (tag); the operator should be able to set another “alarming level”.  
1: Alarms for the whole plant  
2: Alarms for the plant group (production)  
3: Default - alarms for the department (BioTech)  
4: Alarms for the line (Oil factory)  
5: Alarms for the current picture (Tricanter)

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [December 7, 2012, 9:13am UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534/4 "2012-12-07T09:13:51Z")

</div>

Do you know how many levels deep the max alarm is?

---

<div class="post-metadata">

**Author:** ![pat.stave](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@pat.stave](https://forum.inductiveautomation.com/u/pat.stave)\
**Post date:** [December 11, 2012, 5:43pm UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534/5 "2012-12-11T17:43:11Z")

</div>

Hi Nathan,

The string would normally (and max) be 5 levels deep.

---

<div class="post-metadata">

**Author:** ![Robert.McKenzie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/robert.mckenzie/32/4134_2.png) [@Robert.McKenzie](https://forum.inductiveautomation.com/u/Robert.McKenzie)\
**Post date:** [December 12, 2012, 8:17pm UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534/6 "2012-12-12T20:17:37Z")

</div>

So you can use the runScript() function to make this work.  
Note: I used a text field for the string and a spinner for the number of levels deep.`runScript("'/'.join('"+{Root Container.Text Field.text}+"'.split('/', "+{Root Container.Spinner.intValue}+")[:-1])")`This will give you every level possible except the actual tag name. You can cap the spinner at 4 (number of levels - 1).

---

<div class="post-metadata">

**Author:** ![pat.stave](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@pat.stave](https://forum.inductiveautomation.com/u/pat.stave)\
**Post date:** [December 13, 2012, 12:02pm UTC](https://forum.inductiveautomation.com/t/breaking-up-a-string-in-a-expression/5534/7 "2012-12-13T12:02:43Z")

</div>

Thank you Robert, just what i was looking for 😃
