Welcome! Log In Create A New Profile


string$ function

Posted by g8jcf 
string$ function
August 04, 2014 02:18AM

Has anybody got a really quick ByPic function/plugin for the string$ function present in most BASICs ? ie string$(Count, Char) where Count is the number of characters to return, and Char is the character, eg X$=string$(20,"0") would result in X$ containg 20 zero characters, ie "00000000000000000000".

The best I can do is
//Returns a string l long of c$ characters
function string$(l,c$)
    dim a$[255],i,j
    for i=1 to l
    next i

    return a$

I tried poke'ing asc(c$,0) into a$ instead of appending c$, but the strlen was always incorrect.

Many thanks

Re: string$ function
August 05, 2014 01:00AM

This seems to work and is much quicker.

//Returns a string l long of c$ characters
function string$(l,c$)
    dim a$[64],i,c
    c = asc(c$,0)
    for i=?a$+4 to ?a$+l+4
        pokec (i,c))
    next i
    //Append null to terminate string
    pokec (i,0x00)
    return a$
endf  //string$

The trick is that the first 4 bytes of the string, ie ?A$+0, ?A$+1, ?A$+2, ?A$+3, are not character bytes, but appear to relate to string length, once I'd figured that out by peekc'ing the string, it became simple. This fact is probably mentioned in the documentation, but I must have missed it.

Hope this helps

Sorry, you do not have permission to post/reply in this forum.