Page 1 of 1

FillString

Posted: Wed Nov 20, 2013 1:32 pm
by NikitaOdnorob98

Code: Select all

Procedure$ FillString(Symbol$, Lenght)
  Protected String$ = ""
  Protected i
  For i = 1 To Lenght
    String$ + Symbol$
  Next
  ProcedureReturn String$
EndProcedure

a$ = FillString("1", 10)
Debug a$

Re: FillString

Posted: Wed Nov 20, 2013 1:35 pm
by luis

Code: Select all

Debug LSet("",10, "1")

Re: FillString

Posted: Wed Nov 20, 2013 1:37 pm
by Bisonte
Sorry.... but

Code: Select all

b$ = RSet("", 10, "1") ; or b$ = LSet("", 10, "1")
Debug b$
Edit : Arg to slow ;)

Re: FillString

Posted: Wed Nov 20, 2013 1:45 pm
by PB
And don't forget...

Code: Select all

a$=ReplaceString(Space(10)," ","1")
Debug a$
...which has the added avantage of using multiple chars:

Code: Select all

a$=ReplaceString(Space(10)," ","ho")
Debug a$ ; Santa laughing!

Re: FillString

Posted: Wed Nov 20, 2013 1:56 pm
by davido
Would something like this work:

Code: Select all

*B = AllocateMemory(10000001)

For M = 0 To 1000000
  PokeB(*B + M,192)
Next M
PokeB(*B + 1000000,0)
A$ = PeekS(*B)
I guess it could be improved by using integers.

Re: FillString

Posted: Wed Nov 20, 2013 2:41 pm
by luis
davido wrote: I guess it could be improved by using integers.

I think this is better:

. works with unicode
. it is faster for a large buffer because it uses FillMemory()
. does not loop using Poke()

PS: you don't need to poke a NULL at the end of the buffer, AllocateMemory reset the area to zeros.

Code: Select all

len = 10 ; len of the string
size = len * SizeOf(Character)
*p = AllocateMemory(size + SizeOf(Character))
FillMemory(*p, size, Asc("?"), #PB_Character)


ShowMemoryViewer(*p, size + SizeOf(Character))
a$ = PeekS(*p)
Debug Len(a$)
Debug a$
But LSet() is good enough and easier, especially for small strings.. and does not need an additional PeekS() at the end to transfer the data to a PB string (if required).

Re: FillString

Posted: Wed Nov 20, 2013 3:48 pm
by davido
Thanks luis,

I did think there was a better way. Just didn't know how. :)

Re: FillString

Posted: Wed Nov 20, 2013 3:53 pm
by wilbert
A fixed length string could also be an option

Code: Select all

MyString.s{10}
FillMemory(@MyString, 10 * SizeOf(Character), Asc("?"), #PB_Character)

Debug MyString

Re: FillString

Posted: Wed Nov 20, 2013 7:47 pm
by STARGÅTE
Here my Code:

Code: Select all

Define MyString.s{10}
FillMemory(@MyString, SizeOf(MyString), 'X', #PB_Character)

Debug MyString

Re: FillString

Posted: Wed Nov 20, 2013 8:16 pm
by davido
Hi STARGÅTE,

As expected: Concise and incredibly fast!

Re: FillString

Posted: Wed Nov 20, 2013 8:41 pm
by luis
Note in the original code the length was a parameter, using a static string you have to specify a constant for its size, so it's not the same thing.