How to left pad a string with spaces

I’am searching for a string function wich filled up the string to the left side.

problem:

I need a string with fixed lenght a.e. string[10]. A.e. i have an input string with 5 character [a b c d e]. Now i will automaticly filled up ist with [x x x x x a b c d e]. The x schould be blanks. I search an function witch can do this dynamicly. The lenght of the input string can i find out with the function brsstrlen. But for the filling up with blanks ich have no idea.

Have somebody done such function or an idea to solve this problem?

Best regards dirk

Hi,

there some different possibilities to solve it, here’s one quick try as proposal from my side.

Before:

After:

And here’s the sample task, of course this could / should be wrapped into a library:
StrTest.zip (1.3 KB)

Best regards!

5 Likes

PROGRAM _INIT

brsmemcpy(ADR(str_in),ADR('abcde'),5);
brsmemcpy(ADR(str_in_last),ADR(str_in),5);
brsmemcpy(ADR(str_out),ADR('          '),10);
brsmemcpy(ADR(str_out)+5,ADR(str_in),5);

END_PROGRAM

PROGRAM _CYCLIC

IF (brsmemcmp(ADR(str_in),ADR(str_in_last),5) <> 0) THEN
	brsmemcpy(ADR(str_out)+5,ADR(str_in),5);
	brsmemcpy(ADR(str_in_last),ADR(str_in),5);
END_IF

END_PROGRAM

VAR
str_out : STRING[10];
str_in : STRING[5];
str_in_last : STRING[5];
END_VAR

If this a structured text program, you could also take a look at the STANDARD library and the functions INSERT,LEN,MID,LEFT,RIGHT,DELETE and CONCAT.