# String Replace in Painless

**URL:** <https://discuss.elastic.co/t/string-replace-in-painless/326231>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 22, 2023, 9:53pm UTC](https://discuss.elastic.co/t/string-replace-in-painless/326231 "2023-02-22T21:53:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Warner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_warner/32/117609_2.png) [@John\_Warner](https://discuss.elastic.co/u/John_Warner)\
**Post date:** [February 22, 2023, 9:53pm UTC](https://discuss.elastic.co/t/string-replace-in-painless/326231/1 "2023-02-22T21:53:46Z")

</div>

I tried the following replace, and it did not work. Seems to be returning the empty string. Is this the correct way to do a string replace? Note that what I found on this thread does not seem to work for me. [Replace string in a string field (Ingest Pipeline)](https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553)

```auto
for(def hit: ctx.payload.hits.hits) {
    formattedMessage = hit._source.message.replace('{', '<')
    formattedMessage = formattedMessage.replace('}', '>')
    logs.add(['id':hit._id, 
              'msg':hit._source.message,
              'formattedMessage':formattedMessage]);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 22, 2023, 9:54pm UTC](https://discuss.elastic.co/t/string-replace-in-painless/326231/2 "2023-03-22T21:54:09Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
