# Groovy syntax question

**URL:** <https://community.smartthings.com/t/groovy-syntax-question/154189>\
**Category:** Writing SmartApps\
**Created:** [February 14, 2019, 3:05pm UTC](https://community.smartthings.com/t/groovy-syntax-question/154189 "2019-02-14T15:05:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerrydiaz11](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@jerrydiaz11](https://community.smartthings.com/u/jerrydiaz11)\
**Post date:** [February 14, 2019, 3:05pm UTC](https://community.smartthings.com/t/groovy-syntax-question/154189/1 "2019-02-14T15:05:32Z")

</div>

Hi–

I’m having a bit of a time figuring out the proper syntax to parse the value from between the “[]”.  
For example:

`x = currentState.value`  
returns  
`"[on]"`  
How do I get just the “on” value without resorting to a crude replace method?

Thank you.

---

<div class="post-metadata">

**Author:** ![philh30](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@philh30](https://community.smartthings.com/u/philh30)\
**Post date:** [February 14, 2019, 4:10pm UTC](https://community.smartthings.com/t/groovy-syntax-question/154189/2 "2019-02-14T16:10:22Z")

</div>

Assuming I have a switch declared in my smartapp as mySwitch, the following returns “x=on”

```auto
def x=mySwitch.currentState("switch")
log.debug "x=${x.value}"

```

---

<div class="post-metadata">

**Author:** ![gvitzi](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/gvitzi/32/101958_2.png) [@gvitzi](https://community.smartthings.com/u/gvitzi)\
**Post date:** [February 14, 2019, 9:03pm UTC](https://community.smartthings.com/t/groovy-syntax-question/154189/3 "2019-02-14T21:03:50Z")

</div>

You could strip the first and last characters using substring:

> x = currentState.value  
> x = ​​​​​​​x.substring(1, x.size() - 1)

---

<div class="post-metadata">

**Author:** ![jerrydiaz11](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@jerrydiaz11](https://community.smartthings.com/u/jerrydiaz11)\
**Post date:** [February 15, 2019, 4:03am UTC](https://community.smartthings.com/t/groovy-syntax-question/154189/4 "2019-02-15T04:03:55Z")

</div>

Thank you guys for your replies.

@philh30: your example copied verbatim returns:

> ` myVersion=[on]`

@gvitzi: Your code returns:

> ```
> groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.substring() is applicable for argument types: (java.lang.Integer, java.lang.Integer) values: [1, 0]
> Possible solutions: toString(), toString(), toString(), toString(), subList(int, int), subList(int, int) 
> 
> ```

So I’m sorry to report I remain in the same boat. Can anyone please offer a solution? I’m sure this can’t be the an unusual need or not have a specific means to return just the value without brackets.

---

<div class="post-metadata">

**Author:** ![gvitzi](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/gvitzi/32/101958_2.png) [@gvitzi](https://community.smartthings.com/u/gvitzi)\
**Post date:** [February 15, 2019, 5:37am UTC](https://community.smartthings.com/t/groovy-syntax-question/154189/5 "2019-02-15T05:37:52Z")

</div>

From the exception you got, it appears that currentState.value returns an ArrayList  
Meaning that [on] is a list with one item “on”

In this case you could get the first item this way:

> x = currentState.value[0]

---

<div class="post-metadata">

**Author:** ![jerrydiaz11](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@jerrydiaz11](https://community.smartthings.com/u/jerrydiaz11)\
**Post date:** [February 15, 2019, 6:09am UTC](https://community.smartthings.com/t/groovy-syntax-question/154189/6 "2019-02-15T06:09:44Z")

</div>

Which essentially works out to:

> **YOU,** @gvitzi = _ **officially** _ my **hero**!

Thank you!!!
