# String.format help

**URL:** <https://community.particle.io/t/string-format-help/16309>\
**Category:** Firmware\
**Created:** [October 7, 2015, 2:10am UTC](https://community.particle.io/t/string-format-help/16309 "2015-10-07T02:10:55Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hootie81](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hootie81/32/2879_2.png) [@Hootie81](https://community.particle.io/u/Hootie81)\
**Post date:** [October 7, 2015, 2:10am UTC](https://community.particle.io/t/string-format-help/16309/1 "2015-10-07T02:10:55Z")

</div>

Hey Guys, im trying to work out the new String.format(); that was added in 0.4.6

im trying to simplify this

```cpp
        String voltage = String(lipo.getVoltage(), 2);
        String soc = String(lipo.getSOC(), 1);
        Particle.publish("Battery", String(voltage + "V " + soc + "%"), 60, PRIVATE);

```

Any idea’s?  
this gives an error (pointing to a line 5 lines above)

```cpp
        Particle.publish("Battery", String.format("%1.2fV %2.1f%%" , lipo.getVoltage(), lipo.getSOC()), 60, PRIVATE);

```

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [October 7, 2015, 2:24am UTC](https://community.particle.io/t/string-format-help/16309/2 "2015-10-07T02:24:09Z")

</div>

@Hootie81,

there might be some issue there. I tested using the unit-testing example and it failed as well:

> <https://github.com/particle-iot/firmware/blob/f95cf1e5502aa4b435a1b7e42c4fb702d2fb369b/user/tests/unit/string.cpp>

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [October 7, 2015, 2:33am UTC](https://community.particle.io/t/string-format-help/16309/3 "2015-10-07T02:33:04Z")

</div>

Something like this worked:

```auto
void setup(){
        String oldstr = "";
        String newstr = oldstr.format("%1.2f %s %s please", 3.120102, "hey", "hey");
        Particle.publish("new","newstr");
}

```

Not sure why the `String.format` is throwing an error.

---

<div class="post-metadata">

**Author:** ![Hootie81](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hootie81/32/2879_2.png) [@Hootie81](https://community.particle.io/u/Hootie81)\
**Post date:** [October 7, 2015, 6:46am UTC](https://community.particle.io/t/string-format-help/16309/4 "2015-10-07T06:46:00Z")

</div>

Ahhhh silly me! it took me half a day the other day to figure out why i couldn’t get String.toInt() to work… then i realized “String” refers to the String variable name rather than typing String!

And this is exactly the same!

```cpp
String data = "";

Particle.publish("Event", data.format("Yay %s", "it works"), 60, PRIVATE);

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2015, 8:02am UTC](https://community.particle.io/t/string-format-help/16309/5 "2015-10-07T08:02:15Z")

</div>

But it might also be useful to have a static method `format()` (and others) in `String` to allow what you tried to do.

```auto
Particle.publish("Battery", String.format("%1.2fV %2.1f%%" , lipo.getVoltage(), lipo.getSOC()), 60, PRIVATE);

```

How about this @mdma?

---

<div class="post-metadata">

**Author:** ![jvanier](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jvanier/32/5542_2.png) [@jvanier](https://community.particle.io/u/jvanier)\
**Post date:** [October 7, 2015, 4:35pm UTC](https://community.particle.io/t/string-format-help/16309/6 "2015-10-07T16:35:57Z")

</div>

@Hootie81 You need to use the double colon to call the method on the String class.

```auto
Particle.publish("Battery", String::format("%1.2fV %2.1f%%", lipo.getVoltage(), lipo.getSOC()), 60, PRIVATE);

```

@kennethlimcp how did the unit test example fail for you?

As far as I know @ScruffR it wouldn’t be possible to make `String.format` do the right thing in C++. It has to be `String::format`.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2015, 4:46pm UTC](https://community.particle.io/t/string-format-help/16309/7 "2015-10-07T16:46:57Z")

</div>

@jvanier, I guess I’ve confused it with C# 😊 (day job language)

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [October 7, 2015, 5:07pm UTC](https://community.particle.io/t/string-format-help/16309/8 "2015-10-07T17:07:57Z")

</div>

Anyways the example for the docs: [https://docs.particle.io/reference/firmware/photon/#format-](https://docs.particle.io/reference/firmware/photon/#format-)

Doesn’t work.

Unit test runs fine but not when i use the same line in the Web IDE.

`String newStr = String.format("%d %s %s please", 3, "lemon", "curries")==String("3 lemon curries please"));`

---

<div class="post-metadata">

**Author:** ![jvanier](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jvanier/32/5542_2.png) [@jvanier](https://community.particle.io/u/jvanier)\
**Post date:** [October 7, 2015, 7:56pm UTC](https://community.particle.io/t/string-format-help/16309/9 "2015-10-07T19:56:34Z")

</div>

Try

```auto
String newStr = String::format("%d %s %s please", 3, "lemon", "curries");
bool equal = (newStr == String("3 lemon curries please"));

```

Notice the :: in `String::format`

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [October 7, 2015, 11:50pm UTC](https://community.particle.io/t/string-format-help/16309/10 "2015-10-07T23:50:33Z")

</div>

My point is, the docs is giving a wrong example that everyone can simply copy, compile and stab themselves in the eye.

Issue opened here: [https://github.com/spark/docs/issues/203](https://github.com/spark/docs/issues/203)

---

<div class="post-metadata">

**Author:** ![Hootie81](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hootie81/32/2879_2.png) [@Hootie81](https://community.particle.io/u/Hootie81)\
**Post date:** [October 8, 2015, 12:47am UTC](https://community.particle.io/t/string-format-help/16309/11 "2015-10-08T00:47:22Z")

</div>

Thanks @jvanier ill set up another photon to test this… way too much awesome data coming in from my water monitor now 🙂 my last shower was 70L at a rate of 15L/min hrmm time for a new water saving shower head i think!

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [October 8, 2015, 11:54am UTC](https://community.particle.io/t/string-format-help/16309/12 "2015-10-08T11:54:22Z")

</div>

There was a typo in the docs - now fixed. I’d really prefer to avoid the `::` but not sure how to do that short of declaring a global instance that folks can use. E.g.

```auto
static String string;
string.format("hello %s","world");

```

And I think this would cause further confusion if someone mistypes `String` for `string`.

---

<div class="post-metadata">

**Author:** ![jvanier](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jvanier/32/5542_2.png) [@jvanier](https://community.particle.io/u/jvanier)\
**Post date:** [October 8, 2015, 4:11pm UTC](https://community.particle.io/t/string-format-help/16309/13 "2015-10-08T16:11:37Z")

</div>

The easiest workaround might be to document a free function `FormattedString(const char *format, ...)` that calls the static `String::format`

Since `format` is static it’s actually has the surprising behavior right now that you can do `String x; x.format("%d", 10");` but that doesn’t use `x` in any way. It doesn’t mutate `x` and assign it the formatted value. I’m not saying it should, but it’s puzzling.

Providing a free standing format function and not documenting the static `String::format` would solve both of these issues: how to create a new formatted string and avoid the surprise that calling format on an instance doesn’t mutate that instance.

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [October 8, 2015, 4:19pm UTC](https://community.particle.io/t/string-format-help/16309/14 "2015-10-08T16:19:10Z")

</div>

these are good points. I also wonder if format could be an instance method, so

```auto
String().format(...);

```

and

```auto
String msg;
msg.format(...);

```

work as expected.

---

<div class="post-metadata">

**Author:** ![jvanier](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jvanier/32/5542_2.png) [@jvanier](https://community.particle.io/u/jvanier)\
**Post date:** [October 8, 2015, 4:23pm UTC](https://community.particle.io/t/string-format-help/16309/15 "2015-10-08T16:23:45Z")

</div>

Since format hasn’t been in the firmware for that long it should be OK to change it to an instance method. `String().format(...)` does look a little weird however…

From a memory management perspective, it makes a lot of sense because in many use cases you can create a global String then use `format` on it in the `loop()`. You don’t need to re-allocate the buffer if it’s big enough.

The other possibility is to make a varargs contructor to String (I’m not sure if that’s even possible) and ditch `format` altogether.

---

<div class="post-metadata">

**Author:** ![Hootie81](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hootie81/32/2879_2.png) [@Hootie81](https://community.particle.io/u/Hootie81)\
**Post date:** [October 8, 2015, 4:30pm UTC](https://community.particle.io/t/string-format-help/16309/16 "2015-10-08T16:30:24Z")

</div>

Im getting some crashes now using String::format(), well i think its that anyway… SOS and 5 flashes - Usage Fault hrmmm any ideas?
