Announcement

Collapse
No announcement yet.

How to split a string into parts?

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

  • How to split a string into parts?

    Hello @All

    I have a string which I want to split into several parts
    All the parts are seperates with a semicolon ';'. How can I split this string into several strings like this with truncing the spaces?

    Example:
    source string is: "1; -106732; 783743; GL; Cvn;"

    result should be like this

    String1 "1"
    Srting2 "-106732"
    String3 "783743"
    String4 "GL"
    String5 "Cvn"

  • #2


    The attached code will get you roughly where you want to be. You just need to strip spaces off after the split operation.

    The code contains a workround for a v1.05 PmmC str_Find() bug. Just delete the commented line once str_Find() has been fixed (hopefully in v1.05b!)

    Regs
    Steve Attached files split.4dg (1.6 KB)

    Comment


    • #3


      Many many thanks!
      I'm not so familiar with string handling in C or 4DGL. Normally I use Delphi where you have a lot of funtions to handly any action with strings.

      Comment


      • #4


        No problem, glad to help.

        Here's a little routine for removing leading spaces...

        Add it to the demo prog provided

        func LeftTrim(var wpstr)
        var lth, bpstrs, bpstrd, fnd, idx;

        bpstrs := str_Ptr(wpstr); // Raise string pointer for source string pointer
        bpstrd := bpstrs; // Take copy for destination string pointer
        lth := str_Length(wpstr); // Get length of string

        // Move source pointer to first non-space character
        fnd := 0;
        while(str_GetByte(bpstrs + fnd++) == ' ');

        // If space found then shift the remaining bytes left to overwrite the spaces
        if(fnd > 0)
        bpstrs := bpstrs + fnd - 1;
        idx := 0;
        repeat
        str_PutByte(bpstrd + idx, str_GetByte(bpstrs + idx));
        until(++idx >= lth);
        endif

        endfunc




        Now just insert the following on the line after the call to GetNextSub()...

        LeftTrim(mysub)

        It's a bit raw, but it'll do the trick. (It ought to have a length check in it to prevent a loop when all spaces are passed in to it.) Let's say that is an exercise for the reader!!

        Regards
        Steve

        Comment


        • #5
          Thank you for the code. it works fine.
          But i Want to store the string parts in a string array



          Code:
          //global vars
          
             var ObjInfo[6];       // Pointer array for infos
             var ObjInfo0[20];     //string arrays for info strings
             var ObjInfo1[20];
             var ObjInfo2[20];
             var ObjInfo3[20];
             var ObjInfo4[20];
             var ObjInfo5[20];
          
          func readKatalog(var idx)
          var bpnext; 
          var ind;
          var p; //String pointer 
          
             
              file_Rewind(F);
              file_Seek(F,0,(idx*RECSIZE));
          
             
              file_GetS(rec,100,F);
              ind := 0;
              txt_MoveCursor(16,1);
          
              // Point to string to extract sub-strings from
              bpnext := str_Ptr(rec);
              repeat
                  if((bpnext:=GetNextSub(bpnext, mysub, ';')) != 0)
                     // print("Info ", ind, "=[", [STR] mysub, "]\n");
                      p:=str_Ptr(ObjInfo[ind]);               //pointer to ObjInfo record with index ind
                      to(p); print([STR] mysub);      //Print string to Array identifies by pointer p
                      ind++;
                  endif
              until(bpnext == 0);
              
          //output
              print("Index : ",[STR] ObjInfo0,"\n"); 
              print("RA    : ",[STR] ObjInfo1,"\n");
              print("DE    : ",[STR]ObjInfo2,"\n");
              print("Type  : ",[STR]ObjInfo3,"\n");
              print("Const.: ",[STR]ObjInfo4,"\n");
              print("Mag.  : ",[STR]ObjInfo5,"\n");
          endfunc
          
          
          func main()
          
              //initialization of ObjInfo array
              ObjInfo[0]:= &ObjInfo0[20];
              ObjInfo[1]:= &ObjInfo1[20];
              ObjInfo[2]:= &ObjInfo2[20];
              ObjInfo[3]:= &ObjInfo3[20];
              ObjInfo[4]:= &ObjInfo4[20];
              ObjInfo[4]:= &ObjInfo5[20];
          
          repeat
             ....
             
             func readKatalog(1)
             
             ....
          forever
          
          endfunc
          And this will end in a RESET of the display? :-(
          I think I have a problem with understanding the handling of arrays and pointers in 4DGL.


          Comment


          • #6


            Hi Thomas,

            You are very close! You just need to send the print direct to the Word pointer array rather that raising a byte pointer for the string, i.e;

            to(ObjInfo[ind]); print([STR] mysub);

            Regards
            Steve

            Comment


            • #7


              Ah it works

              Comment

              Working...
              X