LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to de delete everthing between square bracket in a string

I try to build an application to delete everything between <> in a string,below is my code, the shortage of my code is it can not deal with the string like
<LL>Serial Number: <BB = "COURIER NEW">NONE</JJ>
but only
<LL>Serial Number: <BB = "COURIER NEW">NONE
(please note the difference)
That is to say, it can not deal the right part of <>.
Thanks!
Jacky

//Cut everything between <> , for example:
  • Serial Number: NONE
    void CutAllBetweenSquareBrackets(char *s,char *resultS)
    {
    int i, j, p,n,a,b,c;
    a=(int) '<';
    b=(int) '>';

    n=strlen(s);
    for (i=0; i
    {
    c=(int) s[i];
    if (a==c)
    {
    for (j=i+1; j
    {
    c=(int) s[j];
    if (b==c)
    {
    Mid(s,resultS,j+2,1000);strcpy(s,resultS); n=strlen(s);i=-1;break;
    }
    else continue;

    }
    }
    }

    }

    //Get a substring from a string, from pointer n
    void Mid(char *s,char *resultS,int n,int m)
    {
    int i,j,p;
    if(n<1) n=1;
    i=strlen(s);
    if(i
    if(m<0) m=i;
    else m=n+m-1;
    if(m>i) m=i;
    p=m-n+1;
    if(p<0) p=0;
    for(i=n-1,j=0;i
    {resultS[j]=s[i]; }
    resultS[p]=0;
    }
  • 0 Kudos
    Message 1 of 2
    (3,158 Views)

    A simple solution could use ANSI strtok ( ) function. I wrote this little code you can try even in the Interactive window. It needs to be trimmed a little to accomodate input and output to your actual application, but it should work as expected (note that the delimiter used changes from call to call to strtok function):

    #include <utility.h>
    #include <ansi_c.h>
    static char src[256], *item;

    strcpy (src, "<LL>Serial Number: <BB = \042COURIER NEW\042>NONE</JJ>");
    DebugPrintf ("Source string: %s\n", src);
    item = strtok (src, ">");
    if (!item) DebugPrintf ("No delimiter found\n");
    while (1) {
     item = strtok (NULL, "<");
     if (!item) break;
     DebugPrintf ("%s", item);
     item = strtok (NULL, ">");
     if (!item) break;
    }
    DebugPrintf ("\nEnd of source string\n");

    The output in the debug window is as follows:

     Source string: <LL>Serial Number: <BB = "COURIER NEW">NONE</JJ>
     Serial Number: NONE
     End of source string



    Proud to use LW/CVI from 3.1 on.

    My contributions to the Developer Community
    ________________________________________
    If I have helped you, why not giving me a kudos?
    0 Kudos
    Message 2 of 2
    (3,142 Views)