# Slicing a String

**URL:** <https://forum.inductiveautomation.com/t/slicing-a-string/59845>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [May 20, 2022, 4:34pm UTC](https://forum.inductiveautomation.com/t/slicing-a-string/59845 "2022-05-20T16:34:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![christgustavoe](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@christgustavoe](https://forum.inductiveautomation.com/u/christgustavoe)\
**Post date:** [May 20, 2022, 4:34pm UTC](https://forum.inductiveautomation.com/t/slicing-a-string/59845/1 "2022-05-20T16:34:32Z")

</div>

Hello,

I´m reading a string tag over OPC connection:

{  
“Job222222Guid”: “863b5079-6c42-4f3f-a865-bda506d7a555”,  
“ExternalJobGuid”: “00000000-0000-0000-0000-000000000000”,  
**"Name": “JOB069216-002”,**  
“Description”: “#FormatType#X3000Y1200”,  
“UserInfo1”: “”,  
“UserInfo2”: “”,  
“UserInfo3”: “”,  
“MeasurementSystem”: 0,  
“NcProgramFile”: “S:\JOB069216-002.LCC”  
}

I created a new ExpressionTag to read only the Job Name:

substring({[.]…/Work/CurrentJob.value},133,142)

the substring is **JOB069216** was expected, i have a problem when the number of caracteres are incremented or decremented because my substring doesnt works right.

I need to do something like Slicing but i dont have this expression inside the tags, anyone already solved this problem ?? Anyone have some idea or example ??

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [May 20, 2022, 4:39pm UTC](https://forum.inductiveautomation.com/t/slicing-a-string/59845/2 "2022-05-20T16:39:33Z")

</div>

What about using Split?  
[https://docs.inductiveautomation.com/display/DOC81/split](https://docs.inductiveautomation.com/display/DOC81/split)

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [May 20, 2022, 6:06pm UTC](https://forum.inductiveautomation.com/t/slicing-a-string/59845/3 "2022-05-20T18:06:31Z")

</div>

I’ll start off here by saying that using `split` as @josborn mentions might be the best solution. However, your inquiry got me thinking what it might look like to keep those associated pieces with the original tag. So I thought I’d share this since it was a fun little academic/thought exercise:

[https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/f/7f444b94f42aa072ea38c7fe27985acf01c044e6.mp4](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/f/7f444b94f42aa072ea38c7fe27985acf01c044e6.mp4)

> **Tag Change Script content**
>
> ```python
> def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
> import re
> # Use named groups in the regex that match your custom property definitions
> p = re.compile('^(?P<JobName>JOB\d+)-(?P<JobSuffix>.*)$')
> # Attempt a match against the current value
> m = p.match(currentValue.value)
> # If we've got a match...
> if (m is not None):
> # pull named groups out of match and assemble list of tag paths
> tag_paths = ['{0}.{1}'.format(tagPath, key) for key in m.groupdict().keys()]
> # pull associated values from named groups for list of tag values
> tag_values = [val for val in m.groupdict().values()]
> # write to local custom properties
> system.tag.writeBlocking(tag_paths, tag_values)
> else:
> # ... otherwise, perform error handling in the event of a non-match
> pass
> 
> ```

I think I’d probably hoist this functionality out into a more generalized script library function that I could invoke with a much simpler call syntax (i.e. one line) in the tag event script if I were to go down this route on a production app. But like I said at the onset, keeping it simple definitely has a LOT of advantages. 🙂

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 20, 2022, 6:14pm UTC](https://forum.inductiveautomation.com/t/slicing-a-string/59845/4 "2022-05-20T18:14:11Z")

</div>

Since the value is JSON I’d expect you could use the [`jsonGet`](https://docs.inductiveautomation.com/display/DOC81/jsonGet) expression function before splitting on “-” to remove it. Or maybe I missed something.
