Technique to convert a string to "words" (i.e. separates text based on a space)

Date:8 December 2005
Product/Release:Visual LANSA V11 RDMLX only
Abstract:This tip shows how to separate text based on the spaces within the text. The technique displays some simple string handling techniques available in Visual LANSA V11
Submitted By:LANSA Technical Support

This tip shows how to separate text based on the spaces with in the text. The technique displays some simple string handling techniques available in Visual LANSA Version 11. This is best explained with an example.

  • Create a form named "mytip" or something similar (do not name it "words")
  • Cut and paste the following code into the form and compile and run
  • Enter a sentence into the field at the top of the form
  • Each word is separated out and added to the list
  • Refer to in-line code comments to gain an understanding of how this is used

Code:

Function Options(*DIRECT)
BEGIN_COM ROLE(*EXTENDS #PRIM_FORM) CAPTION('Convert a sentence to individual words') CLIENTHEIGHT(313) CLIENTWIDTH(695) LEFT(74) TOP(126) WIDTH(703)
DEFINE_COM CLASS(#STD_QORD.Visual) NAME(#Sentence) CAPTION('Enter sentence') DISPLAYPOSITION(1) HEIGHT(25) LABELTYPE(Caption) LEFT(8) MARGINLEFT(100) PARENT(#COM_OWNER) TABPOSITION(1) TOP(8) USEPICKLIST(False) WIDTH(681)
DEFINE_COM CLASS(#PRIM_LTVW) NAME(#LV_Words) COLUMNBUTTONHEIGHT(20) COMPONENTVERSION(2) DISPLAYPOSITION(2) FULLROWSELECT(True) HEIGHT(255) LEFT(8) PARENT(#COM_OWNER) SHOWSORTARROW(True) TABPOSITION(2) TOP(40) WIDTH(680)
DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_1) CAPTION('Words') CAPTIONTYPE(Caption) DISPLAYPOSITION(1) PARENT(#LV_Words) SOURCE(#STD_QSEL) WIDTH(21) WIDTHTYPE(Remainder)

Evtroutine Handling(#Sentence.Changed) Options(*NOCLEARMESSAGES *NOCLEARERRORS)
Clr_List Named(#lv_words)
For Each(#Word) In(#Com_owner.StringToWords( #Sentence ))
#Std_Qsel := #Word
Add_Entry To_List(#lv_words)
Endfor
Endroutine

Mthroutine Name(StringToWords)
Define_Map For(*Input) Class(#Prim_alph) Name(#String)
Define_Map For(*Result) Class(#prim_lcol<#Prim_alph>) Name(#Words) Pass(*by_reference)
Define_Com Class(#prim_nmbr) Name(#FirstSpace)
Set_Ref Com(#words) To(*create_as #prim_lcol<#prim_alph>)

* Loop until nothing left in the string to process
Dountil (#String.cursize = 0)

* Drop any leading and trailing spaces
#String := #String.trim

* Look for the first space in the string
#FirstSpace := #string.PositionOf( ' ' )

* If no space found, must be a whole word
If (#FirstSpace = 0)

#FirstSpace := #String.cursize
Endif
#Words.insert( #Com_owner.AddWord( #String.Substring( 1 #FirstSpace ) ) )

* Remove the word from the string
#String := #String.DeleteSubstring( 1 #FirstSpace )
Enduntil
Endroutine

Mthroutine Name(AddWord) Access(*private)
Define_Map For(*Input) Class(#Prim_alph) Name(#Word)
Define_Map For(*Result) Class(#Prim_alph) Name(#Result) Pass(*by_reference)
Set_Ref Com(#Result) To(*create_as #Prim_alph)
#Result.value := #Word
Endroutine
End_Com