# Help with string manipulation

**URL:** <https://discourse.nodered.org/t/help-with-string-manipulation/49958>\
**Category:** General\
**Created:** [20 August 2021 15:39 UTC](https://discourse.nodered.org/t/help-with-string-manipulation/49958 "2021-08-20T15:39:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![warnert](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/warnert/32/108132_2.png) [@warnert](https://discourse.nodered.org/u/warnert)\
**Post date:** [20 August 2021 15:39 UTC](https://discourse.nodered.org/t/help-with-string-manipulation/49958/1 "2021-08-20T15:39:21Z")

</div>

I am sending some data to a display with 2 lines of 16 char's.  
The top line I want to display the amount of Watts taken from the net and the second line the returned to the net. So I want the first string to be exactly 16 chars. I came up with this but that doesn't work in a function node:

```auto
var meterreceived = global.get('meter_received');
var regel1 = "Watt in : " + global.get('meter_cons');
var lregel1 = regel1.lenght;
for (var i = lregel1; i < 16; i++) regel1 = regel1 + " ";
msg.payload = regel1 + "terug: " + meterreceived;

```

No spaces are inserted when the line is less than 16 chars.  
meter\_cons is something between 0 and 40000.  
I think I have the for wrong but I cannot find someting else.

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [20 August 2021 15:52 UTC](https://discourse.nodered.org/t/help-with-string-manipulation/49958/2 "2021-08-20T15:52:59Z")

</div>

I think you could try `padStart(maxLength, fillString)`. So something like

```auto
let text = "Watt in : ";
let regel1 = text + global.get('meter_received').padStart(16 - text.length,' ');

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 August 2021 15:54 UTC](https://discourse.nodered.org/t/help-with-string-manipulation/49958/3 "2021-08-20T15:54:31Z")

</div>

> [@warnert](#):
>
> `var lregel1 = regel1.lenght`

You have a typo there.

---

<div class="post-metadata">

**Author:** ![warnert](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/warnert/32/108132_2.png) [@warnert](https://discourse.nodered.org/u/warnert)\
**Post date:** [20 August 2021 16:03 UTC](https://discourse.nodered.org/t/help-with-string-manipulation/49958/4 "2021-08-20T16:03:13Z")

</div>

oops... i didn't see that.. the problem of being a Dutchman.... now it turns orange...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 October 2021 16:03 UTC](https://discourse.nodered.org/t/help-with-string-manipulation/49958/5 "2021-10-19T16:03:19Z")

</div>

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