c - 替换字符问题仍未解决

标签 c replace

同时

GetWindowText(spotify_window_handle, title, title_length)

输出包含替换字符。

Spotify - Philip Glass � Glassworks Opening

我需要用“-”替换“�”。我前段时间发布了这个问题但是没有粘贴我的代码所以回答我假设我使用 wchar_t 作为标题,但我不是。标题是 char*。 任何帮助将不胜感激,我是 C 编码的“新手”,有时我在处理一些基本/奇怪的行为时遇到很大困难。

这是我的代码:

char* spotify_title(int window_handle)
{
    int title_length = GetWindowTextLength(window_handle);
        if(title_length != 0)
        {
            char* title;
            title = (char*)malloc((++title_length) * sizeof *title );
            if(title != NULL)
            {
                GetWindowText(window_handle, title, title_length);
                if(strcmp(title, "Spotify") != 0)
            {
                return title;
            }
            else
            {
                return "Spotify is not playing anything right now. Type !botnext command to restart playback.";
            }
        }
        else
        {
            printf("PLUGIN: Unable to allocate memory for title\n");
        }
        free(title);
    }
    else
    {
        printf("PLUGIN: Unable to get Spotify window title\n");
    }
}
// End of Spotify get title function

最佳答案

根据您上面的评论,我假设该字符是 0x96 = En Dash 字符。

#define TITLE_PREFIX "Spotify - "
if (strncmp(title, TITLE_PREFIX, strlen(TITLE_PREFIX) == 0)
{    char *pp = strchr(title + strlen(TITLE_PREFIX), 0x96); // 0x96 = En Dash character
     if (pp != NULL)
         *pp = '-';
}

关于c - 替换字符问题仍未解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122760/

相关文章:

c - 定义结构类型的数组

c - 信号和库

c++ - 如何部署C静态库: header + c file or object file?

linux - 查找并替换 .htaccess 代码

c - 如何将第一个符号移到最后?

c++ - InternetGetLastResponseInfo 返回奇怪的字符而不是错误消息

mysql - MySQL 中的 `REPLACE` 和 `INSERT ... ON DUPLICATE KEY UPDATE` 之间有什么实际区别?

java - 在 Java 中更新集合项

swift - 在 Swift 中清理长文本时面临许多边缘情况

java - 在 Java 中替换后,阿拉伯文本中的英语对齐方式发生变化