gpt4 book ai didi

c++ - 如何只解析字符串的最后一个字符?

转载 作者:行者123 更新时间:2023-11-30 18:38:07 25 4
gpt4 key购买 nike

我目前正在研究的是从输入的 mp3 字符串中解析出歌曲标题和艺术家。

该字符串的格式为“artist-title.mp3”,我们的老师向我们提供了此信息,并确保任何歌曲名中都不会出现 - 字符,但有标题中可能是也可能不是 . 字符。

我现在编写的方式与 strtok() 函数完美配合,只抓取这些字符之前的内容并将它们放入标题/艺术家字段中。

像这样:

//Parse out the artist and title from the song
char* artistName = strtok(song, "-");
char* songName = strtok(NULL, ".");

//Allocate a new block of memory to the blocks songInfo fields with the correct size
newBlock->songInfo.artist = (char*)malloc(sizeof(char) * strlen(artistName) + kNullTerminator);
newBlock->songInfo.title = (char*)malloc(sizeof(char) * strlen(songName) + kNullTerminator);

唯一的问题是,如果标题名称中有一个点,它不会将完整的字符串识别为标题,而是将其截断。我想知道的是如何使用 strtok() 获取最后一个 . 而不仅仅是它找到的第一个点?

最佳答案

首先,您可以使用strrchr()找出输入中最后一次出现的 .

也就是说,使用 strtok(),您也可以完成此操作。只要继续解析相同的字符串,直到得到 NULL,然后考虑最后一次的情况。

关于c++ - 如何只解析字符串的最后一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34886427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com