# space()

**URL:** <https://forum.xojo.com/t/space/25307>\
**Category:** General\
**Created:** [July 4, 2015, 11:45pm UTC](https://forum.xojo.com/t/space/25307 "2015-07-04T23:45:03Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![javier\_ledantes](https://forum.xojo.com/user_avatar/forum.xojo.com/javier_ledantes/32/4847_2.png) [@javier\_ledantes](https://forum.xojo.com/u/javier_ledantes)\
**Post date:** [July 4, 2015, 11:45pm UTC](https://forum.xojo.com/t/space/25307/1 "2015-07-04T23:45:03Z")

</div>

hola (hello)

estoy buscando una funcion igual a space() en visual basic 6  
Esta funcion permitia completar espacios en blanco a una cadena de texto, si tienen conocimiento de alguna en xojo lo agradeceria.

Replacement space () VB6 to xojo

complete blank characters

ejem

“la casa roja” ---------\> 4 espacios "la casa roja "

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [July 4, 2015, 11:52pm UTC](https://forum.xojo.com/t/space/25307/2 "2015-07-04T23:52:49Z")

</div>

I’m not sure google translate is helping very much with this, what exactly does the space() function do? From the translation I got it seems like you’re looking for the literal space character.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 5, 2015, 12:06am UTC](https://forum.xojo.com/t/space/25307/3 "2015-07-05T00:06:37Z")

</div>

There is no equivalent for VB’s Space() function in Xojo. Mostly because there is rarely a need for it. What do you need it for? You may find out you don’t actually need to use it. If you do, though, it is easy to create:

```auto
Function Space(n as integer) as string
   static sp as string = " "
   while len(sp)< n
      sp = sp + sp
   wend
   return left(sp, n)
End Function
```

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 5, 2015, 12:07am UTC](https://forum.xojo.com/t/space/25307/4 "2015-07-05T00:07:51Z")

</div>

And my initial reaction to the post title: the\_final\_frontier()

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 5, 2015, 8:12am UTC](https://forum.xojo.com/t/space/25307/5 "2015-07-05T08:12:34Z")

</div>

[quote=198613:@javier ledantes]hola (hello)

estoy buscando una funcion igual a space() en visual basic 6  
Esta funcion permitia completar espacios en blanco a una cadena de texto, si tienen conocimiento de alguna en xojo lo agradeceria.

Replacement space () VB6 to xojo

complete blank characters

ejem

“la casa roja” ---------\> 4 espacios "la casa roja "[/quote]

Tim ha postado una manera. Une otra para cadenas limitadas en longitud, probablemente mas rapida, es :

`Function Space(espacios as integer) as String
Return Left(" ", espacios)
end Function`

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [July 5, 2015, 8:22am UTC](https://forum.xojo.com/t/space/25307/6 "2015-07-05T08:22:49Z")

</div>

[quote=198630:@Michel Bujardet]Tim ha postado una manera. Une otra para cadenas limitadas en longitud, probablemente mas rapida, es :

`Function Space(espacios as integer) as String
Return Left(" ", espacios)
end Function`[/quote]  
That assumes he never wants more than the number of spaces provided in the string.

Could be problematic. I had cases where I needed a padding of a few hundred spaces (dealing with megaprimers).

Tim’s solution would work even then.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 5, 2015, 8:36am UTC](https://forum.xojo.com/t/space/25307/7 "2015-07-05T08:36:28Z")

</div>

[quote=198632:@Markus Winter]That assumes he never wants more than the number of spaces provided in the string.

Could be problematic. I had cases where I needed a padding of a few hundred spaces (dealing with megaprimers).

Tim’s solution would work even then.[/quote]

I apologize for Spanish, I did not notice that was in general. I did indicate “for strings limited in length, probably faster”. The idea being that if he often needs strings that are less than a certain length, using Left() directly will be faster than a loop.

All depends what he needs. Sometimes a tack suffices, and no need to use a nail and a hammer 😉

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 5, 2015, 8:44am UTC](https://forum.xojo.com/t/space/25307/8 "2015-07-05T08:44:50Z")

</div>

[quote=198613:@javier ledantes]hola (hello)

estoy buscando una funcion igual a space() en visual basic 6…/…"[/quote]

Favor de poner preguntas en Espanol en el canal de mismo numbre. General es en Ingles.

---

<div class="post-metadata">

**Author:** ![Alain\_Bailleul](https://forum.xojo.com/user_avatar/forum.xojo.com/alain_bailleul/32/1370_2.png) [@Alain\_Bailleul](https://forum.xojo.com/u/Alain_Bailleul)\
**Post date:** [July 5, 2015, 8:49am UTC](https://forum.xojo.com/t/space/25307/9 "2015-07-05T08:49:03Z")

</div>

Or something like this:

```auto
Function Space(Number as integer) As String
  dim s() as string
  redim s(number)
  Return join(s, " ")
End Function
```

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 5, 2015, 8:57am UTC](https://forum.xojo.com/t/space/25307/10 "2015-07-05T08:57:52Z")

</div>

[quote=198636:@Alain Bailleul]Or something like this:

`
Function Space(Number as integer) As String
  dim s() as string
  redim s(number)
  Return join(s, " ")
End Function
`[/quote]

Very nice 🙂

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 5, 2015, 12:08pm UTC](https://forum.xojo.com/t/space/25307/11 "2015-07-05T12:08:28Z")

</div>

[quote=198636:@Alain Bailleul]Or something like this:

`
Function Space(Number as integer) As String
  dim s() as string
  redim s(number)
  Return join(s, " ")
End Function
`[/quote]

I like it! My guess is that Tim’s version would be faster, so time to test…

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 5, 2015, 12:13pm UTC](https://forum.xojo.com/t/space/25307/12 "2015-07-05T12:13:14Z")

</div>

I should point out that both StringUtils and M\_String have a Repeat function that can be used for this like:

```auto
Repeat_MTC( " ", 100 )
```

M\_String is at

[http://www.mactechnologies.com/downloads](http://www.mactechnologies.com/downloads)

---

<div class="post-metadata">

**Author:** ![Alain\_Bailleul](https://forum.xojo.com/user_avatar/forum.xojo.com/alain_bailleul/32/1370_2.png) [@Alain\_Bailleul](https://forum.xojo.com/u/Alain_Bailleul)\
**Post date:** [July 5, 2015, 12:59pm UTC](https://forum.xojo.com/t/space/25307/13 "2015-07-05T12:59:07Z")

</div>

> [@198647:@Kem Tekinay](#):
>
> I like it! My guess is that Tim’s version would be faster, so time to test…

Definitely. I just thought it was a rather elegant solution. 🙂

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 5, 2015, 9:01pm UTC](https://forum.xojo.com/t/space/25307/14 "2015-07-05T21:01:11Z")

</div>

I test three methods. First, Alain’s suggestion, second, the implementation in M\_String and StringUtils, and finally, Tim’s suggestion. That also worked out slowest to fastest:

The code:

[code  
const kRepetitions = 1000  
const kSpaceCount = 100

#if not DebugBuild  
#pragma BackgroundTasks False  
#pragma NilObjectChecking False  
#pragma StackOverflowChecking False  
#pragma BoundsChecking False  
#endif

dim msg as string  
dim sw as new Stopwatch\_MTC  
sw.Start

for i as integer = 1 to kRepetitions  
dim arr() as string  
redim arr( kSpaceCount )  
dim s as string = join( arr, " " )  
next

sw.Stop  
msg = “With arr: " + format( sw.ElapsedMicroseconds, “#,” ) + " microsecs”  
AddToResult msg

sw.Reset  
sw.Start

for i as integer = 1 to kRepetitions  
dim spaces as string = " "  
dim l as integer = 1

```
dim halfLen as integer = ( kSpaceCount + 1 ) \\ 2
while l < halfLen
  spaces = spaces + spaces
  l = l + l
wend
dim diff as integer = kSpaceCount - l
dim s as string = spaces + spaces.Left( diff )

```

next

sw.Stop  
msg = “Doubling: " + format( sw.ElapsedMicroseconds, “#,” ) + " microsecs”  
AddToResult msg

sw.Reset  
sw.Start

dim spaces as string = " "  
dim spacesLen as integer = spaces.Len

for i as integer = 1 to kRepetitions  
while spacesLen \< kSpaceCount  
spaces = spaces + spaces  
spacesLen = spacesLen + spacesLen  
wend  
dim s as string = spaces.Left( kSpaceCount )  
next

sw.Stop  
msg = “Remembering: " + format( sw.ElapsedMicroseconds, “#,” ) + " microsecs”  
AddToResult msg  
[/code]

Typical results from a compiled app, two consecutive runs:

```auto
With arr: 2,715 microsecs
Doubling: 1,684 microsecs
Remembering: 301 microsecs
With arr: 2,574 microsecs
Doubling: 1,639 microsecs
Remembering: 321 microsecs
```

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 5, 2015, 10:12pm UTC](https://forum.xojo.com/t/space/25307/15 "2015-07-05T22:12:52Z")

</div>

A note about my suggestion: since the string of spaces is static, subsequent calls to space() will only incur additional overhead if the requested number of spaces is larger than the currently stored string. It quickly approaches parity with Michel’s suggestion, regardless of requested length.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 5, 2015, 10:20pm UTC](https://forum.xojo.com/t/space/25307/16 "2015-07-05T22:20:33Z")

</div>

Also, that suggestion did not originate with me. It was posted by someone much smarter on the old forum.

---

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [July 5, 2015, 11:00pm UTC](https://forum.xojo.com/t/space/25307/17 "2015-07-05T23:00:19Z")

</div>

If you can assume space is always a byte then using the B version halves the time

[code] sw.Reset  
sw.Start

dim spacesB as string = " "  
dim spacesLenB as integer = spaces.LenB

for i as integer = 1 to kRepetitions  
while spacesLenB \< kSpaceCount  
spacesB = spacesB + spacesB  
spacesLenB = spacesLenB + spacesLenB  
wend  
dim s as string = spaces.LeftB( kSpaceCount )  
next

sw.Stop  
msg = “RememberingB: " + format( sw.ElapsedMicroseconds, “#,” ) + " microsecs”  
log msg[/code]

Remembering: 718 microsecs  
RememberingB: 336 microsecs

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 6, 2015, 2:23am UTC](https://forum.xojo.com/t/space/25307/18 "2015-07-06T02:23:26Z")

</div>

There is another function in VB of a similar nature with similar problematics : String$() , or string() with the format  
String$(number, character)

I have adapted it as strings() to RB way back when, rather than `Space()`, because it is more generalized.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:49pm UTC](https://forum.xojo.com/t/space/25307/19 "2020-10-29T17:49:55Z")

</div>


