# String Manipulation

**URL:** <https://forum.inductiveautomation.com/t/string-manipulation/11153>\
**Category:** General Discussion\
**Created:** [February 1, 2016, 4:42pm UTC](https://forum.inductiveautomation.com/t/string-manipulation/11153 "2016-02-01T16:42:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dturner](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dturner](https://forum.inductiveautomation.com/u/dturner)\
**Post date:** [February 1, 2016, 4:42pm UTC](https://forum.inductiveautomation.com/t/string-manipulation/11153/1 "2016-02-01T16:42:14Z")

</div>

I am trying to manipulate a string from an OPC tag using the Left function. I am able to return the full string without issue. After several failed attempts, I copied the text from the Hello World example given for the left function as a test and it also returned an error stating “NameError: name ‘left’ is not defined”. Am I missing something?

Expression from example:  
x = left(“hello”, 2) - I put the x = into the expression.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 1, 2016, 4:47pm UTC](https://forum.inductiveautomation.com/t/string-manipulation/11153/2 "2016-02-01T16:47:14Z")

</div>

It sound like you are trying to use this in a script. Maybe a bit more detail on what you’re trying to do is in order.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 1, 2016, 5:00pm UTC](https://forum.inductiveautomation.com/t/string-manipulation/11153/3 "2016-02-01T17:00:37Z")

</div>

Just to iterate, scripting (Jython) doesn’t have a left function. Instead you would use what’s called slicing:

Here’s an overview in the Python [documentation on strings](https://docs.python.org/2/tutorial/introduction.html#strings)

---

<div class="post-metadata">

**Author:** ![dturner](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dturner](https://forum.inductiveautomation.com/u/dturner)\
**Post date:** [February 1, 2016, 9:43pm UTC](https://forum.inductiveautomation.com/t/string-manipulation/11153/4 "2016-02-01T21:43:00Z")

</div>

I am reading the Windows machine name from several clients. Each client’s machine name is in the form of xxxx\_yyy\_Name. The only portion I am interested in using is the “Name”. I got the left function from Appendix B -\> Strings -\> Left from the Help menu.

This is the example given in the help.

```auto
left(string, charCount)

Returns count characters from the left side of string, where count and string are the arguments to the function.

left("hello", 2)

...returns "he"

left("hello", 0)

...returns ""

left("hello", 5)

...returns "hello"
```

This is what I was using to test.

```auto
strMachine_Name = system.tag.read("[System]Client/Network/Hostname")
strClientName = left(strMachine_Name.value, 9)
print(strClientName)
```

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [February 1, 2016, 10:07pm UTC](https://forum.inductiveautomation.com/t/string-manipulation/11153/5 "2016-02-01T22:07:58Z")

</div>

You need to understand the difference between Jython scripting and Igntion’s built in expression language.

In expression language “left” is a valid function however you can not create and assign values to variables. If you want to assign the left 5 chars in a binding you simply put left(“hello world”, 5) in the expression.

Scripting is actual Jython code and you have to use string slicing as Jordan indicated.

```auto
myString = "hello world"
x = myString[:5]
print x
```

Are you trying to get the machine name in an expression or in an event script? Once you determine the answer to that question we can be of more assistance as needed.

Edit: It looks like you are trying to use Jython scripting.

This is what you need: (I also removed the unnecessary parenthesis in the print statement)

```auto
strMachine_Name = system.tag.read("[System]Client/Network/Hostname")
strClientName = strMachine_Name.value[9:]
print strClientName
```

---

<div class="post-metadata">

**Author:** ![dturner](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dturner](https://forum.inductiveautomation.com/u/dturner)\
**Post date:** [February 1, 2016, 10:26pm UTC](https://forum.inductiveautomation.com/t/string-manipulation/11153/6 "2016-02-01T22:26:17Z")

</div>

Thank you! This is exactly what I am looking for.
