Announcement

Collapse
No announcement yet.

Appending to a string.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Appending to a string.

    I am finding it impossible to append, let alone change a single character in a string. Am I just missing something or can you not modify an already defined array of characters. This snippet of code shows that I can access the specified array element, but I can't modify it.

    func main() gfx_Set(SCREEN_MODE,LANDSCAPE) ; var c; c := "This is a Test"; print ( "Variable = "); print ( [CHR] c[5] ); c[0] := '3'; print ( [STR] c ); pause(8000);endfunc

  • #2


    Variables and array elements are 16 bits long, which should make it a bit easier to understand why your code behaves the way it does.

    There are many string handling examples to be found in these forums, there are also examples here http://www.4dsystems.com.au/code/index.php?node=78

    And of course in the supplied examples.
    Mark

    Comment


    • #3


      I'm actually finding it very hard to find an example where an array element is being modified without modifying the rest of the string. That example with APPEND is exactly what I needed, but the previously mentioned problem eludes me altogether.

      Comment


      • #4


        From the manual I would guess str_PutByte() would do that
        Mark

        Comment


        • #5


          Please note that 4DGL is very similar to C in the fact that
          var c; c := "This is a Test";

          sets c to point to a hard coded string in the code memory space that cannot be altered.


          If you wish to manipulate a string you need to get the static string into memory like so:-

          var buffer[10]; // 10 word array can hold 20 chars

          to(buffer); putstr("This is a Test"); // copy a static string to word aligned memory

          then you can access individual characters using a string pointer, eg:

          var p;
          p := str_Ptr(c); // raise a string pointer to word aligned memory
          putch(str_GetByte(p+3)); // will priint the 's'
          Regards,
          Dave

          Comment


          • #6


            Thank you both. PutByte definitely did it and the string pointer let me navigate and manipulate the string or rather, array of characters. Thanks!

            Comment

            Working...
            X