How to delete spaces after last letter from a string

Hi,

I get a message like this in a string from my code.

string mess = ¨abcdef ¨;

In some other situation, the code gives me
string mess=¨abcd ¨;

the length of mess variable is always 16 .
I need to program with arduino code some way to delete all the spaces from mess after the last letter. How could I don that?

Thank you

(deleted)

See…

There is no need for this forum… Just Google.

Your string should be declared as:char mess[] = "abcdef          ";
Just replace the first space by a null character.

jbellavance:
Your string should be declared as:char mess[] = "abcdef          ";
Just replace the first space by a null character.

That won't work if there are more letters beyond the first space.

Better to work backwards from the end of the string.

Would the trim() function do what you want ?

EDIT : just realised that C does not have such a function, which seems to be a strange omission

in code

void stripTrailingSpaces(char *s)
{
  int le = strlen(s) - 1;
  while (le >=0 && s[le] == ' ') le--;
  le++;
  s[le] = '\0';
}

some test strings
char test1 = "a a a a a aa aaaaaaaaaaaaaaaaaaaaa aaaaa aaa a a a a ";

char test2 = " ";

char test3 = "";

one could add a test to check if (s == NULL) return;

slightly faster

void stripTrailingSpaces1(char *s)
{
  int le = strlen(s);
  for (int i = le - 1; i >= 0; i--)
  {
    if (s[i] == ' ') s[i] = '\0';
    else break;
  }
}

As it is a String then you could use

int p = mess.indexOf(" ", 0);
if (p != -1) {
    partmess = mess.substring(0, p);
}
// will give issues if your original message contains spaces.!

or

mess.trim();