Announcement

Collapse
No announcement yet.

Substring from a string

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

  • Substring from a string

    Hello all,I'm new at this, I'm looking for some help regarding substring. Basically, I want the code to extract a substring from a long string based on an index and a separator.examplemy instring = hello,123,test,abc,555
    if I call the function : getSubString(instring,",
    ",3) my result should be: test

    my inputs are the string itself, the separator which is a comma in this case and field index which is 3 in this case.
    Thank you all.
    -Halim

  • #2


    Hi Halim,
    Have a look at this example code

    http://www.4dsystems.com.au/code/index.php?node=78&id=100

    There are several functions there that should give you an idea or two.
    Regards,
    Dave

    Comment


    • #3
      Here is a test I'm trying to play with. I'm trying to get the first characters of a string right before the first dot (.).
      Example:
      mystring= Test.1234.Hello
      myf should be = Test
      I don't know what's wrong with this code, my final result myf is always blank.

      func teststring(var mystring)
      var myf[20];
      var myf_ptr;
      var mystring_ptr;
      mystring_ptr :=str_Ptr(mystring);
      myf_ptr := str_Ptr(myf);
      n := str_Length(mystring_ptr);
      print("\nmy string size: ", n);
      while (n>1)
      cc := str_GetByte(mystring_ptr);
      if (cc == '.')
      n:=0;
      else
      str_CatN(myf_ptr, cc,1);
      mystring_ptr++;
      n := n-1;
      endif
      wend
      print("\nmy field:", [STR] myf);
      return myf
      endfunc

      Comment


      • #4
        If your trying to split a string into substrings, try this
        var linebuf[50]; // string buffer for 100 chars
        var n.p.q;
        var strings[20]; // splitpoints
        var mkr := "."; // marker for split points
        to(linebuf); putstr("Test.1234.Hello");
        n := 0;
        p := str_Ptr(linebuf); // point to line buffer containing string to split

        while(str_GetByte(p)) // split the line into substrings
        strings[n++] := p; // saving pointers to each splitpoint
        if(q := str_Find(&p, mkr)) str_PutByte(q,'\0'); // converting markers to '\0's
        p := q+1;
        wend
        strings[n] := 0; // mark last splitpoint end
        // now the array 'strings' holds the indexes of each splitpoint

        so

        putstr(strings[1]); // prints 1234
        Regards,
        Dave

        Comment


        • #5


          I made some changes to your code by replacing the while loop with :
          while (str_GetByte(p)!=0) // split the line into substrings

          Using putstr(strings[1]) or print(strings[1]) did not give me 1234.

          Thank you for all your help.
          -Halim

          Comment


          • #6
            Hi Halim,
            glad you got it going.
            Apologies for the poor example, I posted it in the wee hours without testing.
            Of course, putstr and print only work on even word boundaries so the example was doomed.

            Here it it corrected:-

            func main()
            var n,p,q;
            var strings[20]; // splitpoints
            var mkr := "."; // marker for split points
            var linebuf[50]; // string buffer for 100 chars

            to(linebuf); putstr("Test.1234.Hello.World");

            n := 0;
            p := str_Ptr(linebuf); // point to line buffer containing string to split

            while(str_GetByte(p)) // split the line into substrings
            strings[n++] := p; // saving pointers to each splitpoint
            if(q := str_Find(&p, mkr)) str_PutByte(q,'\0'); // converting markers to '\0's
            p := q+1;
            wend

            while(n--)
            p := strings[n]; // print the strings in reverse
            str_Printf(&p,"%s\n");
            wend


            repeat forever

            endfunc


            Regards,
            Dave

            Comment

            Working...
            X