space()

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 "

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.

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:

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

And my initial reaction to the post title: the_final_frontier()

[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

[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.

[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 :wink:

[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.

Or something like this:

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

[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 :slight_smile:

[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…

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

Repeat_MTC( " ", 100 )

M_String is at

http://www.mactechnologies.com/downloads

Definitely. I just thought it was a rather elegant solution. :slight_smile:

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:

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

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.

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

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

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.