简体   繁体   English

strsep()函数-分隔符中有更多字符

[英]strsep() function - more characters in delimiter

I am having problems using the strsep() function in C. I want to split a string into two parts. 我在使用C中的strsep()函数时遇到问题。我想将字符串分成两部分。 The string contains info about the currently playing song, in ARTIST - TITLE format, so artist and title are separated by one space, one dash and again one space. 该字符串以ARTIST - TITLE格式包含有关当前播放歌曲的信息,因此艺术家和标题之间用一个空格,一个破折号和一个空格分隔。 I want to separate it by this, " - ". 我想用“-”将其分开。 "-" won't work because some artists have a dash in their name. “-”将不起作用,因为某些艺术家的名字带有破折号。

When I try this code with, for example, "Michel Telo - Ai Se Eu Te Pego": 当我用“ Michel Telo-Ai Se Eu Te Pego”尝试此代码时:

// String is in tmp
while ((token = strsep(&tmp, " - ")) != NULL)
{
    printf("%s\n", token);
}

I get this: 我得到这个:

[root@runeaudio ~]# ./board
Michel
Telo


Ai
Se
Eu
Te
Pego

Instead of this: 代替这个:

[root@runeaudio ~]# ./board
Michel Telo
Ai Se Eu Te Pego

Seems like strsep() is dividing delimiter into 3 characters: " ", "-", " " and using OR between them, but I want it to look for " - " as it is. 似乎strsep()将分隔符分为3个字符:“”,“-”,“”,并在它们之间使用OR,但我希望它按原样查找“-”。 Any idea how to fix this? 任何想法如何解决这个问题?

The following code, demonstrate how you can split the string, it's not very useful because it does nothing with the tokens except for printing them, but you can see how it works and implement a version that does what you need. 下面的代码演示了如何分割字符串,因为它除了打印令牌外对令牌没有任何作用,所以它不是很有用,但是您可以看到令牌的工作方式并实现满足您需要的版本。

char string[] = "Michel Telo - Ai Se Eu Te Pego";
char *separator = strstr(string, " - ");
if (separator != NULL)
 {
     separator[0] = '\0';
     printf("%s\n", string);

     separator[0] = ' ';
     printf("%s\n", separator + 3);
 }

You can of course use strdup() or similar function to create new strings with the contents of the " tokens ". 当然,您可以使用strdup()或类似的函数来创建带有“ 令牌 ”内容的新字符串。

This is of course not robust, because nothing can ensure that there wont be an artist with " - " in it's name, if it's in the song name however, it's not so bad. 这当然不是很可靠,因为没有任何东西可以确保不会有歌手的名字带有" - " ,但是,如果它是歌曲名称,那还不错。

This is a working version, if you don't have strdup() on your platform there will surely be an implementation of it with a different name 这是一个有效的版本,如果您的平台上没有strdup() ,那么肯定会有一个不同名称的实现。

#include <string.h>

void extractArtistAndTitle(char *string, char **artist, char **title)
 {
    char *separator;

    if ((string == NULL) || (artist == NULL) || (title == NULL))
        return;
    separator = strstr(string, " - ");
    if (separator != NULL)
     {
        size_t length;

        length  = separator - string;
        *artist = malloc(1 + length);
        if (*artist != NULL)
        {
            memcpy(*artist, string, length);
           (*artist)[length] = '\0';
        }
        *title = strdup(separator + 3);
     }
 }

int main()
{
    char string[] = "Michel Telo - Ai Se Eu Te Pego";
    char *artist;
    char *title;

    extractArtistAndTitle(string, &artist, &title);
    if (artist != NULL)
        printf("Artist: %s\n", artist);
    if (title != NULL)
        printf("Title : %s\n", title);
    free(artist);
    free(title);

    return 0;
}

Here is a code using which you will only get the strings depending on the '-' 这是一个代码,使用该代码,您只能根据'-'获得字符串

#include <stdio.h>

int main()
{
    char token[100];
    int i,j=0,flag=0;
    char tmp[]="Michel Telo - Ai Se Eu Te Pego";
    for(i=0;tmp[i]!='\0';i++)
    {
        if((tmp[i]>='a' && tmp[i]<='z') || (tmp[i]>='A' && tmp[i]<='Z') || (tmp[i]==32 && !isalpha(tmp[i+1])))
        {
            flag=0;
            token[j++]=tmp[i];
            continue;
        }
        else if(flag==0 && tmp[i]=='-')
        {
            token[j]='\0';
            j=0;
            flag=1;
            printf("%s\n",token);
        }
    }
    token[j]='\0';
    printf("%s\n",token);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM