FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour function to supress intermediate spaces
Posts: 518
Joined: Fri Jun 29, 2012 12:49 PM
function to supress intermediate spaces
Posted: Tue Jan 27, 2015 10:15 AM

Dear friends,

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

Very grateful;).

Posts: 300
Joined: Wed Jul 11, 2007 11:06 AM
Re: function to supress intermediate spaces
Posted: Tue Jan 27, 2015 10:58 AM

Try STRTRAN(var," ","")

Good luck

Posts: 518
Joined: Fri Jun 29, 2012 12:49 PM
Re: function to supress intermediate spaces
Posted: Tue Jan 27, 2015 11:07 AM

Thanks:

function QuitaEspacios( cCadena )

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

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: function to supress intermediate spaces
Posted: Tue Jan 27, 2015 08:39 PM

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

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

Regards



G. N. Rao.

Hyderabad, India
Posts: 1789
Joined: Tue Oct 11, 2005 05:01 PM
Re: function to supress intermediate spaces
Posted: 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 (fw): Select all Collapse
FUNCTION TrimSpace( cString )

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

RETURN cString
Salu2

Carlos Vargas

Desde Managua, Nicaragua (CA)
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: function to supress intermediate spaces
Posted: Wed Jan 28, 2015 03:23 PM

I vote for Jack's version:

STRTRAN(var," ","")

The simpler, the better.

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: function to supress intermediate spaces
Posted: Wed Jan 28, 2015 03:50 PM

Me too! :-)

EMG

Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
Re: function to supress intermediate spaces
Posted: Thu Jan 29, 2015 10:43 AM

Carlos Vargas solution is better

Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: function to supress intermediate spaces
Posted: 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 (fw): Select all Collapse
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
Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
Re: function to supress intermediate spaces
Posted: 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 (fw): Select all Collapse
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
xHarbour 1.2.3 + Fwhh 20.2
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: function to supress intermediate spaces
Posted: Thu Jan 29, 2015 02: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.
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: function to supress intermediate spaces
Posted: Thu Jan 29, 2015 05: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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: function to supress intermediate spaces
Posted: Thu Jan 29, 2015 06:28 PM
nageswaragunupudi wrote:But CharRem() function specially made for such purposes and more efficient.


You're right! Look at this sample:

Code (fw): Select all Collapse
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 (fw): Select all Collapse
         1.36          0
         0.00          0

Wow!

EMG
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: function to supress intermediate spaces
Posted: Thu Jan 29, 2015 06: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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: function to supress intermediate spaces
Posted: Fri Jan 30, 2015 01: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?

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10