How to fix special Turkish characters in a string?

Viewed 699

I have a string extracted from a url as filename that contains special Turkish characters (çğıİöüş) and they seem wrong. How can I fix it?

public static string getFileName(HttpWebResponse response, string url)
{
    var cd = response.Headers["content-disposition"];
    var loc = response.Headers["location"];

    if (!string.IsNullOrEmpty(cd))
    {
        var disp = ContentDispositionHeaderValue.Parse(cd);
        return Uri.UnescapeDataString(disp.FileName);
    }
    else if (!string.IsNullOrEmpty(loc))
        return Path.GetFileName(loc);
    else
        return Path.GetFileName(url);
}

Original String:

y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3

Correct string:

y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3
1 Answers

It seems you've mixed Win-1254 and Utf-8 encodings:

string original =
  @"y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3";

string correct = Encoding.UTF8.GetString(Encoding.GetEncoding(1254).GetBytes(original));

// Let's have a look
Console.Write(correct);

Outcome:

y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3
Related