Ensure you have the string library fix ( for function free(), search for it in the forum ) as you may have memory leaks after calling those methods multiple times.
You could reduce the number of temporary objects and reserve more memory to speed things along though.
From your example:
String StringTrimRight(String InputString, int CharCount) {
String OutputString;
String StringBuffer = InputString;
for (int CharIndex = 0; CharIndex < (StringBuffer.length() - CharCount); CharIndex++) {
OutputString += StringBuffer.charAt(CharIndex);
}
return OutputString;
}
The line 'OutputString += StringBuffer.charAt(CharIndex);' will generate many temporary variables, each one with an allocation.
It can be slightly tweaked:
String StringTrimRight(String InputString, int CharCount) {
const int Steps = InputString.length() - CharCount;
if( Steps < 1 ) return( "" ); //Check for invalid or complete trim
InputString[ Steps - 1 ] = '\0';
return( &InputString[ 0 ] );
}
This leaves the String constructor on exit to do all the looping. And only 1 temporary String object.
And as you do not pass InputString by reference, you can modify its contents without harm.
Passing String objects by reference will boost performance, however you have to copy the data to a buffer if the reference cannot change.
This construct below can help you design code that can use variable sized temporary arrays. So no large global working buffer, or oversized local buffer.
void foo( const int len ){
char temp[ len ];
}