如何从另一个字符串中获取子字符串(C)

时间:2022-10-22 23:08:30

i write a function for find table name in sql query string.

我在sql查询字符串中编写一个查找表名的函数。

my code

我的代码

char* SQLParser_GetTable(char *query)
{
char *str = "";
char *FROM="FROM";    

if(strstr(query, FROM))
{
    char *e;
    int index;      
    e = strchr(query, 'F');
    index = (int)(e - FROM);

    str=substring(str,index+4,5);
}

return  str;
}

main.c

c

     query = "SELECT * FROM TABLE1";
     char *tbl=SQLParser_GetTable(query);

but this code retun full string not table name.

但这段代码是retun完整字符串而不是表名。

MY code must return "TABLE1".

我的代码必须返回“TABLE1”。

2 个解决方案

#1


1  

I don't know what substring() is but this code does what you think yours does

我不知道substring()是什么,但是这段代码做了你认为你做的事情。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *SQLParser_GetTable(char *query)
{
    char *str;
    char *FROM = "FROM ";

    /* point to the start of FORM */
    if ((str = strstr(query, FROM)) == NULL)
        return NULL;
    /* If there is only one space between FROM and TABLE1 point to it */
    str = strchr(str, ' ');
    if (str == NULL)
        return NULL;
    /* move past the ' ' character */
    str += 1;
    /* return a copy of the string */
    return strdup(str);
}

int main ()
{
    char *table = SQLParser_GetTable("SELECT * FROM TABLE1");
    if (table != NULL)
    {
        printf("%s\n", table);
        free(table);
    }
    return 0;
}

notice that this is not robust at all since there can be any number of whitespaces between FROM and the table name.

注意,这一点都不健壮,因为从表名和表名之间可以有任意数量的空白。

#2


0  

I have written the following long function but compared with functions in other answers it is the only one that is more or less correct.:). The function does not take into account the case of letters.:)

我已经写了下面的长函数,但与其他答案的函数相比,它是唯一一个或多或少正确的函数。这个函数没有考虑字母的情况。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char * SQLParser_GetTable( const char *query )
{
    const char FROM[] = "FROM";    
    char *p;
    int found;

    found = ( p = strstr( query, FROM ) ) != NULL;
    found = found && ( p == query || isblank( ( unsigned char )p[-1] ) );
    found = found && ( *( p += sizeof( FROM ) - 1 ) == '\0' ||
                       isblank( ( unsigned char )p[0] ) );

    if ( found )
    {
        const char *q = p;
        size_t n = 0;

        while ( isblank( ( unsigned char )*q ) ) ++q;
        while ( q[n] && !isblank( ( unsigned char )q[n] ) ) ++n;

        p = malloc( ( n + 1 ) * sizeof( char ) );

        memcpy( p, q, n );
        p[n] = '\0';
    }
    else
    {
        p = NULL;
    }

    return p;
}

int main(void) 
{
    char *query = "SELECT * FROM TABLE1";
    char *p = SQLParser_GetTable( query );

    if ( p ) puts( p );

    free( p );

    return 0;
}

The output is

输出是

TABLE1

#1


1  

I don't know what substring() is but this code does what you think yours does

我不知道substring()是什么,但是这段代码做了你认为你做的事情。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *SQLParser_GetTable(char *query)
{
    char *str;
    char *FROM = "FROM ";

    /* point to the start of FORM */
    if ((str = strstr(query, FROM)) == NULL)
        return NULL;
    /* If there is only one space between FROM and TABLE1 point to it */
    str = strchr(str, ' ');
    if (str == NULL)
        return NULL;
    /* move past the ' ' character */
    str += 1;
    /* return a copy of the string */
    return strdup(str);
}

int main ()
{
    char *table = SQLParser_GetTable("SELECT * FROM TABLE1");
    if (table != NULL)
    {
        printf("%s\n", table);
        free(table);
    }
    return 0;
}

notice that this is not robust at all since there can be any number of whitespaces between FROM and the table name.

注意,这一点都不健壮,因为从表名和表名之间可以有任意数量的空白。

#2


0  

I have written the following long function but compared with functions in other answers it is the only one that is more or less correct.:). The function does not take into account the case of letters.:)

我已经写了下面的长函数,但与其他答案的函数相比,它是唯一一个或多或少正确的函数。这个函数没有考虑字母的情况。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char * SQLParser_GetTable( const char *query )
{
    const char FROM[] = "FROM";    
    char *p;
    int found;

    found = ( p = strstr( query, FROM ) ) != NULL;
    found = found && ( p == query || isblank( ( unsigned char )p[-1] ) );
    found = found && ( *( p += sizeof( FROM ) - 1 ) == '\0' ||
                       isblank( ( unsigned char )p[0] ) );

    if ( found )
    {
        const char *q = p;
        size_t n = 0;

        while ( isblank( ( unsigned char )*q ) ) ++q;
        while ( q[n] && !isblank( ( unsigned char )q[n] ) ) ++n;

        p = malloc( ( n + 1 ) * sizeof( char ) );

        memcpy( p, q, n );
        p[n] = '\0';
    }
    else
    {
        p = NULL;
    }

    return p;
}

int main(void) 
{
    char *query = "SELECT * FROM TABLE1";
    char *p = SQLParser_GetTable( query );

    if ( p ) puts( p );

    free( p );

    return 0;
}

The output is

输出是

TABLE1