SA-MP Forums Archive
strip function - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: strip function (/showthread.php?tid=110031)



strip function - StrickenKid - 23.11.2009

I'm trying to make a strip function that will strip leading and trailing spaces and extra spaces from within a string, it should function like this:

input:
Код:
 The quick  brown  fox   jumps    over     the      lazy      dog.
ouput:
Код:
The quick brown fox jumps over the lazy dog.
but the function I made is not taking out the 1 leading space before the first non space character.

function:
pawn Код:
stock strip(const s[])
{
    new
        j = 0,
        tmp[256];
    if(isnull(s))
      return tmp;
    for (new i = 0; (s[i] != '\0' && s[i] != '\t'); i++)
    {
        if ((s[i] == ' ') && (s[i+1] == ' ' || s[i+1] == '\0'))
        {
        }
        else
        {
            tmp[j] = s[i];
            j++;
        }
    }
    return tmp;
}
Anyone know whats wrong?


Re: strip function - -xy! - 24.11.2009

I read the code, and apparently is everything appropriate, just found it strange here:

pawn Код:
tmp[j] = s[i];
  j++;
What did you do here?

Sorry my bad english.





Re: strip function - StrickenKid - 24.11.2009

it copies over the appropriate characters to the new string which is then returned.


Re: strip function - Dabombber - 24.11.2009

Returning a copy of the string might not be the best way since it limits the length of strings the function will work on. I'm not sure if this is exactly what you want since you stop at tabs but oh well.

pawn Код:
stock strip(string[])
{
    if(isnull(string)) return;
    if(ispacked(string)) {
        for(new i; string{i} != '\0'; i++) {
            if(string{i} <= ' ') {
                new end = i;
                while('\0' < string{end + 1} <= ' ') end++;
                if(i == 0) {
                    strdel(string, i, end + 1);
                } else if(string{end + 1} == '\0') {
                    strdel(string, i, end + 1);
                    return;
                } else if(i != end) {
                    strdel(string, i, end);
                }
            }
        }
    } else {
        for(new i; string[i] != '\0'; i++) {
            if(string[i] <= ' ') {
                new end = i;
                while('\0' < string[end + 1] <= ' ') end++;
                if(i == 0) {
                    strdel(string, i, end + 1);
                } else if(string[end + 1] == '\0') {
                    strdel(string, i, end + 1);
                    return;
                } else if(i != end) {
                    strdel(string, i, end);
                }
            }
        }
    }
}



Re: strip function - StrickenKid - 24.11.2009

Thanks Dabomber, I've edited and optimized that code a little, and it works great.

pawn Код:
stock strip(s[])
{
    if (isnull(s))
        return 0;
    for (new i = 0; (s[i] != '\0' && s[i] != '\t' && s[i+1] != '\0'); i++)
    {
        if(s[i] <= ' ')
        {
            new
                end = i;
            while ('\0' < s[end + 1] <= ' ')
                end++;
            if(!i)
                strdel(s, i, end + 1);
            else if(s[end + 1] == '\0')
                strdel(s, i, end + 1);
            else if(i != end)
                strdel(s, i, end);
        }
    }
    return 1;
}