reduce space into a string

Blitz3D Forums/Blitz3D Programming/reduce space into a string

vivaigiochi(Posted 2011) [#1]
I seek for a function that comprime space into a string. that's i want say:

I have the string:
blitzbasic is a good language.

but i want this:

blitzbasic is a good language.


Matty(Posted 2011) [#2]
I can't see any difference between those two strings?


GfK(Posted 2011) [#3]
They look the same to me, too, and I don't know what "comprime" means.

My best guess is that you want to remove leading/trailing spaces from a string, in which case, Trim() is your function.


Yasha(Posted 2011) [#4]
Looking at the page source, the above should be:
blitzbasic     is      a      good language.

but i want this:

blitzbasic is a good language.

(Explanation: comment text outside of code boxes follows the HTML rule that unnecessary spaces are ignored.)

As for code to do what you want... try this:
Local s$ = "blitzbasic     is      a      good language.", s_old$

Repeat
	s_old = s
	s = Replace(s, "  ", " ")
Until Len(s_old) = Len(s)

Print s

WaitKey
End

The loop copies the string, and then replaces every pair of spaces with a single space, comparing the lengths to see if a change was made (if no change, then the string has been compacted fully). The reason it's a loop is because Blitz3D doesn't let Replace() work backwards: give it the above call and four spaces, and it gives two spaces back. So you have to call it again to crush down the result some more.


D4NM4N(Posted 2011) [#5]
Yashas way seems better thatn what i was going to suggest :D
have not tried it like that.

Last edited 2011