trim last character from string? #241
Replies: 7 comments
-
Posted at 2014-03-09 by d0773d I did:
That code removes the last character as expected. In my previous forum post I explained that I attempted to catch the \r by doing:
However, "NO CR" is echoing out 5 times instead of only 4. I have no idea why \r is getting stored to the phValue when the if statement I think should of filtered \r out. Can anyone explain why this is happening? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-03-09 by d0773d I started from scratch and removed the line:
Everything seems to be working properly. I have no idea why, but starting from scratch seemed to do the trick. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-03-10 by @gfwilliams I think your issue was I guess the thing to do is to use the
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-03-10 by d0773d
That was exactly the issue. I read the sensor module user guide again, it explained the results will end in a "\r". So I removed the checking for the newline character. As soon as I removed the newline check and only checked for a CR:
It worked. When I get home from work I will implement your example. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-03-26 by rwaldron If the value is actually a floating point number, but you're receiving a string, then you should be able to simply coerce the value to a number and the non-numeric characters will be discarded:
Unfortunately, that doesn't seem to work correctly in Espruino. I've filed a bug: espruino/Espruino#268 |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-03-26 by @gfwilliams Fixed now ;) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-03-26 by rwaldron claps |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2014-03-08 by d0773d
I read through the reference section of this website and could not find a trim or some sort of replace function. The issue I am having is, my PH sensor responds with a float(ie 8.55) with a length of 5. I think the fifth character is a carriage return. I could not tell so I used String.charCodeAt(5) which responded back with a 13. How do I remove the \r or any none digit character from the end of the float value? Since the value is sent via serial communication I tried:
However, when I check the length of the phValue again I am still getting a length of 5.
Beta Was this translation helpful? Give feedback.
All reactions