Page 1 of 2

function to supress intermediate spaces

Posted: Tue Jan 27, 2015 10:15 am
by elvira
Dear friends,

Is there a function to remove the intermediate spaces from a string ?.

Very grateful;).

Re: function to supress intermediate spaces

Posted: Tue Jan 27, 2015 10:58 am
by Jack
Try STRTRAN(var," ","")

Good luck

Re: function to supress intermediate spaces

Posted: Tue Jan 27, 2015 11:07 am
by elvira
Thanks:

function QuitaEspacios( cCadena )

return strtran( cCadena, Space(1), Space(0) )

Re: function to supress intermediate spaces

Posted: Tue Jan 27, 2015 8:39 pm
by nageswaragunupudi
Also
CharRem( <cDelete>, <cString> ) --> cResult

Examples
CharRem( " ", "One Two Three" ) --> "OneTwoThree"
CharRem( "a", "Banana" ) --> "Bnn"

Re: function to supress intermediate spaces

Posted: Tue Jan 27, 2015 10:18 pm
by carlos vargas
Esta es una función que cambia espacios de mas de uno a uno.
lo uso mucho con instrucciones sql.

Code: Select all

FUNCTION TrimSpace( cString )

   DO WHILE AtNum( "  ", cString ) > 0
      cString := StrTran( cString, "  ", " " )
   ENDDO

RETURN cString
 

Re: function to supress intermediate spaces

Posted: Wed Jan 28, 2015 3:23 pm
by James Bott
I vote for Jack's version:

STRTRAN(var," ","")

The simpler, the better.

Re: function to supress intermediate spaces

Posted: Wed Jan 28, 2015 3:50 pm
by Enrico Maria Giordano
Me too! :-)

EMG

Re: function to supress intermediate spaces

Posted: Thu Jan 29, 2015 10:43 am
by sambomb
Carlos Vargas solution is better

Re: function to supress intermediate spaces

Posted: Thu Jan 29, 2015 12:23 pm
by nageswaragunupudi
carlos vargas wrote:Esta es una función que cambia espacios de mas de uno a uno.
lo uso mucho con instrucciones sql.

Code: Select all

FUNCTION TrimSpace( cString )

   DO WHILE AtNum( "  ", cString ) > 0
      cString := StrTran( cString, "  ", " " )
   ENDDO

RETURN cString
 
For this purpose you may consider using
CHARONE( ' ', cString )
This is a highly optimized function specially meant for removing adjacent duplicate characters.

I know the functions i am talking about are less familiar but the character functions of CT are highly optimized functions.

Full syntax:
CHARONE( [cDelete], cString )

Ex: CharOne( " *-", cStrings ) removes all duplicates of ' ', '*', '-' in one single call.

Re: function to supress intermediate spaces

Posted: Thu Jan 29, 2015 12:30 pm
by sambomb
nageswaragunupudi wrote:
carlos vargas wrote:Esta es una función que cambia espacios de mas de uno a uno.
lo uso mucho con instrucciones sql.

Code: Select all

FUNCTION TrimSpace( cString )

   DO WHILE AtNum( "  ", cString ) > 0
      cString := StrTran( cString, "  ", " " )
   ENDDO

RETURN cString
 
For this purpose you may consider using
CHARONE( ' ', cString )
This is a highly optimized function specially meant for removing adjacent duplicate characters.

I know the functions i am talking about are less familiar but the character functions of CT are highly optimized functions.

Full syntax:
CHARONE( [cDelete], cString )

Ex: CharOne( " *-", cStrings ) removes all duplicates of ' ', '*', '-' in one single call.
Where I can find this function?

Re: function to supress intermediate spaces

Posted: Thu Jan 29, 2015 2:59 pm
by James Bott
Elvira said:
Is there a function to remove the intermediate spaces from a string ?
Nages said:
This is a highly optimized function specially meant for removing adjacent duplicate characters.
I took Elvira's statement to mean that he want to remove all spaces, but I admit it is not clear. Removing adjacent duplicates is a different task.

Re: function to supress intermediate spaces

Posted: Thu Jan 29, 2015 5:45 pm
by nageswaragunupudi
Mr James

Elvira's first posting was for removing all spaces.
StrTran( cString, ' '. '' ) is correct.
But CharRem() function specially made for such purposes and more efficient.

Mr Carlos Vargas' posting was to remove duplicate adjacent spaces. I suggested CharOne() for this instead of the code he was using.

These functions were from the 16-bit Clipper days, included in CA_Tools libaray. At that time, most of these functions were written in Assembly language and highly optimized for speed and size.

Now CT.LIB/HBCT.LIB includes all these functions written mostly in C for (x)Harbour.

Those of us who still have the old Clipper installation on our discs can find the documentation in \clipper\ng. This documentation requirs ng.exe or weg.exe to read.

xharbour.com provided a detailed help file (chm) which contains very good documentation. This is very useful

Re: function to supress intermediate spaces

Posted: Thu Jan 29, 2015 6:28 pm
by Enrico Maria Giordano
nageswaragunupudi wrote:But CharRem() function specially made for such purposes and more efficient.
You're right! Look at this sample:

Code: Select all

FUNCTION MAIN()

    LOCAL cSpaces := SPACE( 100000000 )

    LOCAL nSec := SECONDS()

    cSpaces = STRTRAN( cSpaces, " ", "" )

    ? SECONDS() - nSec, LEN( cSpaces )

    cSpaces = SPACE( 100000000 )

    nSec = SECONDS()

    cSpaces = CHARREM( cSpaces, " " )

    ? SECONDS() - nSec, LEN( cSpaces )

    INKEY( 0 )

    RETURN NIL
I get this:

Code: Select all

         1.36          0
         0.00          0
Wow!

EMG

Re: function to supress intermediate spaces

Posted: Thu Jan 29, 2015 6:47 pm
by cnavarro
Wow! ++++1

Re: function to supress intermediate spaces

Posted: Fri Jan 30, 2015 1:06 am
by James Bott
EMG,

Your computer is fast--2.4 times faster than mine. I get:

3.31
0.03

Either way, charRem() is 100 times faster than strtran().

Thanks Nages.

This is only available with xHarbour though?