On Tue, 25 Sep 2012, Henrique Andrade wrote:

> I would like to know how can I remove the spaces before a specific
> character in a string. Please take a look at my example:
>
> <hansl>
> string s1a = "     @Gretl is great!"
> string s2a = "          @Gretl is really great!"
>
> string s1b = strsub(s1a, "     @", "")
> string s2b = strsub(s2a, "          @", "")
>
> print s1b
> print s2b
> </hansl>
>
> I need to remove the blank spaces before the "@" no matter how many blank
> spaces I have. Does anyone have a suggestion?

1) If you know the starting non-space symbol you can use the 
strstr() function:

<hansl>
string s1b = strstr(s1a, "@")
</hansl>

2) More generally,

<hansl>
loop while strncmp(s1a, " ", 1) == 0 --quiet
   s1a = s1a + 1
endloop
</hansl>

Or via a function:

<hansl>
function string trimstr (string s)
   loop while strncmp(s, " ", 1) == 0 --quiet
     s = s + 1
   endloop
   return s
end function

string s1 = "     @Gretl is great!"
s2 = trimstr(s1)
print s2
</hansl>

Allin Cottrell

Reply via email to