function to supress intermediate spaces

function to supress intermediate spaces

Postby elvira » Tue Jan 27, 2015 10:15 am

Dear friends,

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

Very grateful;).
elvira
 
Posts: 515
Joined: Fri Jun 29, 2012 12:49 pm

Re: function to supress intermediate spaces

Postby Jack » Tue Jan 27, 2015 10:58 am

Try STRTRAN(var," ","")

Good luck
Jack
 
Posts: 280
Joined: Wed Jul 11, 2007 11:06 am

Re: function to supress intermediate spaces

Postby elvira » Tue Jan 27, 2015 11:07 am

Thanks:

function QuitaEspacios( cCadena )

return strtran( cCadena, Space(1), Space(0) )
elvira
 
Posts: 515
Joined: Fri Jun 29, 2012 12:49 pm

Re: function to supress intermediate spaces

Postby nageswaragunupudi » Tue Jan 27, 2015 8:39 pm

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

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10254
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: function to supress intermediate spaces

Postby carlos vargas » Tue Jan 27, 2015 10:18 pm

Esta es una función que cambia espacios de mas de uno a uno.
lo uso mucho con instrucciones sql.

Code: Select all  Expand view

FUNCTION TrimSpace( cString )

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

RETURN cString
 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1688
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: function to supress intermediate spaces

Postby James Bott » Wed Jan 28, 2015 3:23 pm

I vote for Jack's version:

STRTRAN(var," ","")

The simpler, the better.
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Re: function to supress intermediate spaces

Postby sambomb » Thu Jan 29, 2015 10:43 am

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
sambomb
 
Posts: 385
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil

Re: function to supress intermediate spaces

Postby nageswaragunupudi » Thu Jan 29, 2015 12:23 pm

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  Expand view

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
nageswaragunupudi
 
Posts: 10254
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: function to supress intermediate spaces

Postby sambomb » Thu Jan 29, 2015 12:30 pm

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  Expand view

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
sambomb
 
Posts: 385
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil

Re: function to supress intermediate spaces

Postby James Bott » Thu Jan 29, 2015 2:59 pm

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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: function to supress intermediate spaces

Postby nageswaragunupudi » Thu Jan 29, 2015 5:45 pm

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
nageswaragunupudi
 
Posts: 10254
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: function to supress intermediate spaces

Postby Enrico Maria Giordano » Thu Jan 29, 2015 6:28 pm

nageswaragunupudi wrote:But CharRem() function specially made for such purposes and more efficient.


You're right! Look at this sample:

Code: Select all  Expand view
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  Expand view
        1.36          0
         0.00          0

Wow!

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8331
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: function to supress intermediate spaces

Postby cnavarro » Thu Jan 29, 2015 6:47 pm

Wow! ++++1
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6501
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: function to supress intermediate spaces

Postby James Bott » Fri Jan 30, 2015 1:06 am

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?
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 19 guests