# \[SOLVED\] Substring

**URL:** <https://community.openhab.org/t/solved-substring/2876>\
**Category:** Scripts & Rules\
**Tags:** strings, xtend, rules\
**Created:** [September 28, 2015, 6:34pm UTC](https://community.openhab.org/t/solved-substring/2876 "2015-09-28T18:34:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinnaay](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@spinnaay](https://community.openhab.org/u/spinnaay)\
**Post date:** [September 28, 2015, 6:34pm UTC](https://community.openhab.org/t/solved-substring/2876/1 "2015-09-28T18:34:54Z")

</div>

Hi All,

I’m struggling to find any good documentation on Xtend if anyone can point me towards.

I want to a do a substring in a rule but I just can’t find the correct format.

This is so much easier in Python!

What I’m trying is.

```
	val String string1 = 'Hello'	
	val String string2 = string1.subString(0,1)	

```

However this isn’t correct.

Can someone point me to some docs so I can’t read those and refer to them when I need to know a command.

Or can someone rewrite Openhab in Python…

---

<div class="post-metadata">

**Author:** ![George](https://community.openhab.org/letter_avatar_proxy/v4/letter/g/71e660/32.png) [@George](https://community.openhab.org/u/George)\
**Post date:** [September 28, 2015, 6:45pm UTC](https://community.openhab.org/t/solved-substring/2876/2 "2015-09-28T18:45:27Z")

</div>

Simple correction. It is string1.substring(0,1) Notice the lower case S in substring. If you use the Designer interface you can get what is available by pressing ctrl-space. You would do this right after typing string1.

---

<div class="post-metadata">

**Author:** ![spinnaay](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@spinnaay](https://community.openhab.org/u/spinnaay)\
**Post date:** [September 28, 2015, 6:58pm UTC](https://community.openhab.org/t/solved-substring/2876/3 "2015-09-28T18:58:02Z")

</div>

Perfect! Thanks

---

<div class="post-metadata">

**Author:** ![grhosso](https://community.openhab.org/user_avatar/community.openhab.org/grhosso/32/29884_2.png) [@grhosso](https://community.openhab.org/u/grhosso)\
**Post date:** [August 31, 2018, 1:33pm UTC](https://community.openhab.org/t/solved-substring/2876/4 "2018-08-31T13:33:00Z")

</div>

Is this working on rules?

I have this string, and I would like to use it.

```auto
{
  "ip": "aaaa",
  "hostname": "whatever.com",
  "city": "Antartida",
  "region": "Low",
  "country": "VB",
  "loc": "1.1234,1.12345",
  "postal": "12345",
  "org": "AFDSF"
}

```

I wonder how I could use substring, to get for example “loc” or “postal” ??

Thanks.

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [August 31, 2018, 7:46pm UTC](https://community.openhab.org/t/solved-substring/2876/5 "2018-08-31T19:46:43Z")

</div>

Isn;t that JSON?

> **[JsonPath - Transformation Services](https://www.openhab.org/addons/transformations/jsonpath/)**
>
> Extracts values from a JSON string using a JsonPath(https://github.com/jayway/JsonPath#jayway-jsonpath) expression.

---

<div class="post-metadata">

**Author:** ![grhosso](https://community.openhab.org/user_avatar/community.openhab.org/grhosso/32/29884_2.png) [@grhosso](https://community.openhab.org/u/grhosso)\
**Post date:** [August 31, 2018, 7:51pm UTC](https://community.openhab.org/t/solved-substring/2876/6 "2018-08-31T19:51:18Z")

</div>

> [@rossko57](#):
>
> JsonPath - Transformation Services

I do not think so, the whole string is like this, and I would like to extract some words. 😕

```auto
  % Total % Received % Xferd Average Speed Time Time Time Current

                                 Dload Upload Total Spent Left Speed

  0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

100 228 100 228 0 0 678 0 --:--:-- --:--:-- --:--:-- 680

{
  "ip": "aaaa",
  "hostname": "whatever.com",
  "city": "Antartida",
  "region": "Low",
  "country": "VB",
  "loc": "1.1234,1.12345",
  "postal": "12345",
  "org": "AFDSF"
}

```

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [August 31, 2018, 8:52pm UTC](https://community.openhab.org/t/solved-substring/2876/7 "2018-08-31T20:52:05Z")

</div>

Ah OK, some kind of scraping. We only know what you tell us.

I’d be looking for a way to extract the JSON from the whole string, i.e. get the string from { to }. I should think a regex would do that, but sorry I am not familiar.

And then use JsonPath. That way, you’d be fairly safe from the data source changing some other details.

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 31, 2018, 11:35pm UTC](https://community.openhab.org/t/solved-substring/2876/8 "2018-08-31T23:35:23Z")

</div>

There is a nifty string method called `.substringBetween`

```php
rule "another rule"
when
    Item LivingRoom_WallSwitch changed
then
    var String myString = "blahblah{\"ip\": \"aaaa\",\"hostname\": \"whatever.com\",\"city\": \"Antartida\",\"region\": \"Low\",\"country\": \"VB\",\"loc\": \"1.1234,1.12345\",\"postal\": \"12345\",\"org\": \"AFDSF\"}"
    logInfo("TEST", myString)
    var String newString = myString.substringBetween("{","}") //Extract the JSON string between the curly brackets
    newString = "{" + newString + "}" // Add the JSON curly brackets back
    logInfo("TEST", newString)
    var String jsonString = transform("JSONPATH", "$.loc", newString) // Extract the loc value
    logInfo("TEST", jsonString) // Display : 1.1234,1.12345
    jsonString = transform("JSONPATH", "$.postal", newString) //Extract the postal value
    logInfo("TEST", jsonString) // Display: 12345
end

```
