Split String function

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
serge_girard
Posts: 2562
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 855 times
Been thanked: 160 times
Contact:

Split String function

Post by serge_girard »

Anybody has a function to split string into array, based upon a split-character and length?

Example:

Code: Select all

xSTRING := 'This command is used to draw a rectangle from the specified location to a specified another location' //   LEN=100
aRET :=  SPLITSTRING(xSTRING , 30, SPACE(1))

// OUTPUT SHOULD BE LIKE THIS....
? aRET [1] // This command is used to draw
? aRET [2] // a rectangle from the specified 
? aRET [3] // location to a specified 
? aRET [4] // another location  
OR

Code: Select all

xSTRING := 'This command is, used to draw a, rectangle from, the specified location, to a specified, another location' //   LEN=100
aRET :=  SPLITSTRING(xSTRING , 30, ',')

// OUTPUT SHOULD BE LIKE THIS....
? aRET [1] // This command is,
? aRET [2] // used to draw a rectangle,  
? aRET [3] // from, the specified location,
? aRET [4] // to a specified 
? aRET [5] // another location  
The concatenated array should be the same as input string...

Serge

edk
Posts: 586
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 148 times
Been thanked: 515 times

Post by edk »

Maybe something like that ?:

Code: Select all

#include <hmg.ch>
Function Main()
Local xSTRING := 'This command is used to draw a rectangle from the specified location to a specified another location' //   LEN=100
Local aRET :=  SPLITSTRING(xSTRING , 30, SPACE(1))
Local i, cMessage:=""

FOR i := 1 TO Len ( aRet )
	cMessage += aRet [i] + "   »»» Len := " + AllTrim( Str ( Len ( aRet [i] ) ) ) + CRLF
NEXT i
msginfo ( cMessage )

xSTRING := 'This command is, used to draw a rectangle from, the specified location, to a specified, another location' //   LEN=100
aRET :=  SPLITSTRING(xSTRING , 30, ',')

cMessage:=""
FOR i := 1 TO Len ( aRet )
	cMessage += aRet [i] + "   »»» Len := " + AllTrim( Str ( Len ( aRet [i] ) ) ) + CRLF
NEXT i
msginfo ( cMessage )

RETURN Nil

************************************************************************
Function SplitString( cString, nLen, cSplitChar )
Local aOrphans := { "the", "a", "an" }
Local aSplitString := {}
Local cSplitString := ""
Local aTokens, i, cLastWord

Default cString := ""
Default nLen := 1
Default cSplitChar :=  " "

aTokens := hb_ATokens( cString, cSplitChar )

FOR i := 1 TO Len( aTokens )

	IF Len ( cSplitString ) + Len ( aTokens [i] ) <= nLen 
		cSplitString += aTokens [i] + IF ( Len ( cSplitString ) + Len ( aTokens [i] ) < nLen .AND. i < Len( aTokens ) , cSplitChar, "")
	ELSE
		cSplitString := AllTrim ( cSplitString )

		/* don't leave "orphans" (ex. English article) at the end of the string */
		cLastWord := StrTran ( SubStr( cSplitString, RAt ( " ",  cSplitString ) + 1), cSplitChar, "")

		IF AScan ( aOrphans, { |x| Upper(x) == Upper( cLastWord ) } ) > 0
			cSplitString := Left ( cSplitString, Len ( cSplitString ) - Len ( cLastWord ) )
			AADD ( aSplitString, AllTrim ( cSplitString ) )		
			cSplitString := cLastWord + cSplitChar + aTokens [i] + IF ( i < Len( aTokens ) , cSplitChar, "")
		ELSE
			AADD ( aSplitString, AllTrim ( cSplitString ) )		
			cSplitString := aTokens [i] + IF ( i < Len( aTokens ) , cSplitChar, "")
		ENDIF
	ENDIF
NEXT i
AADD ( aSplitString, Alltrim (cSplitString ) )		

RETURN aSplitString


User avatar
serge_girard
Posts: 2562
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 855 times
Been thanked: 160 times
Contact:

Post by serge_girard »

Brilliant !

10 / 10 Super,

Thanks, Serge

Post Reply