Remove unwanted character from serial-string so it does not end up at LCD displa

Hi guys,

I just finished my sketch which seems to be working OK for the most part.
It pulls serial data from an ORP stamp from Atlas Scientific and displays it on the serial plotter fine. With some more code I got it to display on an LCD display as well but the only problem is that it is displaying a character I would like to have removed.

I believe it is a carriage return or new line character that is read from the serial data, but cant be to sure. It looks Korean on the display…

I tried to find a solution and the closest I got was to define a new string and try to remove this character:

inString = inString.replace("\n","");

My code looks a bit different, you can find it below in the full code of the sketch. Close to the end: tst = sensorstring.replace("\n","");

But this does not seem to work for me. I get an error:

exit status 1
no match for ‘operator=’ (operand types are ‘String’ and ‘void’)

I tried left and right to figure it out but am lost. Why is the code not working in my example? What have I done wrong here?

Below my full code:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>  
#include <LiquidCrystal_I2C.h> // Using version 1.2.1
// The LCD constructor - address shown is 0x27 - may or may not be correct for yours
// Also based on YWRobot LCM1602 IIC V1
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Strings clearen
String inputstring = "";
String sensorstring = "";
String tst = "";

// waar of niet waar dat de input_string compleet is op false zetten
boolean input_stringcomplete = false;
boolean sensor_stringcomplete = false;

// Setup
void setup(){

// Init LCD
lcd.begin(16,2); // sixteen characters across - 2 lines

// Init Serial

// Start up the library

sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement


void serialEvent() {
  char inchar = (char);
  inputstring += inchar;
  if(inchar == '\r') {input_stringcomplete = true;}   

void serialEvent3(){
  char inchar = (char);
  sensorstring += inchar;
  if(inchar == '\r') {sensor_stringcomplete = true;}

void loop(){   //here we go....
  if (input_stringcomplete){
    inputstring = "";
    input_stringcomplete = false;

  if (sensor_stringcomplete){

// Temp loop
sensors.requestTemperatures(); // Send the command to get temperatures

// Replace-code which is going wrong during compiling

tst = sensorstring.replace("\n","");

// print to LCD
lcd.print("ORP waarde: " + sensorstring);
lcd.print("Temperatuur: ");

    sensorstring = "";
    sensor_stringcomplete = false;

If you guys could hep getting the replace code to do what I need it to do that would be greatly appreciated. But any other solution to removing the character would also be fine.


Wouldn't it be simpler to not put the character in the String in the first place?

void serialEvent() {
  char inchar = (char);
  if (inchar != '\n')  
    inputstring += inchar;
  if(inchar == '\r') {input_stringcomplete = true;}   

(IMO, It'd be easier to not use Strings at all, but that's another story)

You could also use strchr to proscribe a list of characters, if you so wished.

Why put the character in the String in the first place ? You already test the characters as they are read so could easily not store the offending character(s)

You could also consider using null terminated arrays of chars (aka strings) rather than Strings which may be a good idea in the long term.

The code did not seem to remove the character. It still displays.

void serialEvent3() {
  char inchar = (char);
  if (inchar != '\n')  
    sensorstring += inchar;
  if(inchar == '\r') {sensor_stringcomplete = true;}   

I added it to serialEvent3 since I am displaying ‘sensorstring’ to the LCD, not ‘inputstring’. Or did I make a wrong assumption here? As far as I can tell the serial3 event loads the data outputted by the ORP stamp into sensorstring and that can be used to display it to the LCD.
And serialevent is used to send data to the ORP stamp over the serial port.
Btw I did try it on the other serialevent as you suggested but that didnt change it either.

But it didn’t work either. The character is still added. I have attached a picture of the character to this post. Maybe it helps.

It would be even better if the data could be somehow stored as a number (it runs from -1000 to 1000) so I can use it in the future for more automation. But getting it to display without the character and understanding how it works will be fine for now.

How sure are you that the spurious character is a newline ?

if (inchar != ‘\n’)
sensorstring += inchar;
if(inchar == ‘\r’) {sensor_stringcomplete = true;}

You are STILL adding the \r to the String instance…

How sure are you that the spurious character is a newline?

+1. It’s not a newline.

That character is a special one, outside the normal printable ASCII values of 32 (" “, a space) to 126 (”~", a tilde). The “control” characters, like CR and LF, are values 0 to 31. A char can also have values up to 255, so that weird character is probably >= 128 (127 is the DELETE character).

Your test could be

  if ((' ' <= inchar) && (inchar <= '~'))
   sensorstring += inchar;

This saves all the normal printable characters, and throws away all the control characters and special characters (above 127).

And, like others have mentioned, it would be better to store the characters in a char array, rather than String… it’ll get you sooner or later. See Serial Input Basics for a good example. Here are the relevant lines:

const byte numChars = 32;
char receivedChars[numChars];	// an array to store the received data
		rc =;

		if (rc != endMarker) {
			receivedChars[ndx] = rc; <--- append one char
			ndx++;                   <--- increment the char count
			if (ndx >= numChars) {   <--- make sure you don't store too much (i.e., exceed the array)
				ndx = numChars - 1;
		else {
			receivedChars[ndx] = '\0'; // terminate the string
			ndx = 0;
			newData = true;

This will save 1600 bytes of program space, and countless headaches later.


if ((’ ’ <= inchar) && (inchar <= ‘~’))
sensorstring += inchar;

worked. Thx!

I will have a look at using char array some time in the future and report back here.