function to supress intermediate spaces

elvira
Posts: 462
Joined: Fri Jun 29, 2012 12:49 pm

function to supress intermediate spaces

Post by elvira »

Dear friends,

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

Very grateful;).
Jack
Posts: 249
Joined: Wed Jul 11, 2007 11:06 am

Re: function to supress intermediate spaces

Post by Jack »

Try STRTRAN(var," ","")

Good luck
elvira
Posts: 462
Joined: Fri Jun 29, 2012 12:49 pm

Re: function to supress intermediate spaces

Post by elvira »

Thanks:

function QuitaEspacios( cCadena )

return strtran( cCadena, Space(1), Space(0) )
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: function to supress intermediate spaces

Post by nageswaragunupudi »

Also
CharRem( <cDelete>, <cString> ) --> cResult

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

G. N. Rao.
Hyderabad, India
User avatar
carlos vargas
Posts: 1421
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: function to supress intermediate spaces

Post 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
 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: function to supress intermediate spaces

Post by James Bott »

I vote for Jack's version:

STRTRAN(var," ","")

The simpler, the better.
User avatar
sambomb
Posts: 385
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil
Contact:

Re: function to supress intermediate spaces

Post by sambomb »

Carlos Vargas solution is better
Email: SamirSSabreu@gmail.com
MSN: SamirAbreu@hotmail.com
Skype: SamirAbreu
xHarbour 1.1.0 + FwXh 8.02
xHarbour 1.2.1 + Fwhh 10.6
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: function to supress intermediate spaces

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
sambomb
Posts: 385
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil
Contact:

Re: function to supress intermediate spaces

Post 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?
Email: SamirSSabreu@gmail.com
MSN: SamirAbreu@hotmail.com
Skype: SamirAbreu
xHarbour 1.1.0 + FwXh 8.02
xHarbour 1.2.1 + Fwhh 10.6
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: function to supress intermediate spaces

Post 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.
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: function to supress intermediate spaces

Post 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
Regards

G. N. Rao.
Hyderabad, India
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: function to supress intermediate spaces

Post 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
User avatar
cnavarro
Posts: 5792
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: function to supress intermediate spaces

Post by cnavarro »

Wow! ++++1
C. Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
Si alguien te dice que algo no se puede hacer, recuerda que esta hablando de sus limitaciones, no de las tuyas.
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: function to supress intermediate spaces

Post 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?
Post Reply