Replace consecutive spaces in string by a single space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | *(Q) I have consecutive spaces between words contained within a string variable How can I reduce these to single spaces? *(A) By Raynald Levesque rlevesque@videotron.ca 2002/06/3. * http://pages.infinit.net/rlevesqu/index.htm * Create a sample file for illustration purposes. DATA LIST FIXED /str 1-25 (A). BEGIN DATA jiuhy huji juh kk kk jj jj jjj jj k k l kkk lok kk ii u h kkkk kkkkkk This is my text jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj END DATA. LIST. *////////////////. DEFINE !deldbl(vname=!TOKENS(1) /vlength=!TOKENS(1)) !LET !lookfor=!QUOTE(!BLANK(2)) STRING #tmp !CONCAT('(A',!vlength,')'). COMPUTE #tmp=LTRIM(!vname). LOOP IF RANGE(INDEX(#tmp," "),1,LENGTH(RTRIM(#tmp))). COMPUTE #tmp=CONCAT(SUBSTR(#tmp,1,INDEX(#tmp,!lookfor)), SUBSTR(#tmp,INDEX(#tmp,!lookfor) + 2)). END LOOP. COMPUTE !vname=#tmp. !ENDDEFINE. *////////////////. SET MPRINT=yes. !deldbl vname=str vlength=25. SET MPRINT=no. EXECUTE. |
Related pages
...