Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Remove excessive spaces in string
Message
From
24/07/2009 17:41:55
 
 
To
24/07/2009 17:13:58
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01414370
Message ID:
01414397
Views:
65
>>I have a string that contains many spaces. For example
>>
>>
x = 'test1    test2 test3    test4  test5       test6'
>>
>>how can I format the string to contain only one space between the 'test' values. For example how do get it to do the following:
>>
>>
x = 'test1 test2 test3 test4 test5 test6'
>>
>>I thought about using strtran() command but I don't it works the way I want it too.
>
>Naomi's answer is simple and elegant.
>
>You can do the same thing with successive or nested STRTRAN() calls e.g.
>
>x = 'test1    test2 test3    test4  test5       test6'
>
>x = STRTRAN( x, SPACE( 8 ), SPACE( 1 ) )
>x = STRTRAN( x, SPACE( 8 ), SPACE( 1 ) )
>x = STRTRAN( x, SPACE( 4 ), SPACE( 1 ) )
>x = STRTRAN( x, SPACE( 4 ), SPACE( 1 ) )
>x = STRTRAN( x, SPACE( 2 ), SPACE( 1 ) )
>x = STRTRAN( x, SPACE( 2 ), SPACE( 1 ) )
>
>?x
>* Above handles up to 64 spaces ( 8 x 8 )
>* Adding 2 more lines at the top with SPACE( 16 ) would increase capacity to 256 spaces ( 16 x 16 )
>* etc.
>
>* I suspect the FoxTools REDUCE() function may be a recursive implementation of the above
>
If your going to do that you should just put it in a loop
x = 'test1    test2 test3    test4  test5       test6'
DO WHILE SPACE(2) $ x
   x = STRTRAN(x, '  ', ' ')
ENDDO
Charles

"The code knows no master." - Chuck Mautz
"Everybody is ignorant, only on different subjects." - Will Rogers
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform