Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

encoding - Character problem while getting information from vCard in C#

Here is my vCard content:

BEGIN:VCARD
VERSION:3.0
N:Büyük?ar??ba??o?lu;O?uz;;;
FN:Büyük?ar??ba??o?luO?uz
ORG:;
TEL;CELL;VOICE:019365478236598742588952
END:VCARD

When I try to parse it like this:

var lines = File.ReadAllLines("test.vcf");
var get = lines.FirstOrDefault(x => x.StartsWith("N:"));  
var splt = get?.Replace("N:", "").Split(';').Where(x => !string.IsNullOrEmpty(x)).ToArray();

var first_name = splt != null && splt.Length > 1 ? string.Join(" ", splt.Skip(1).Take(5).ToArray()) : splt?[0];
var last_name = splt != null && splt.Length > 1 ? splt?[0] : string.Empty;

var result = !string.IsNullOrEmpty(last_name) ? first_name + " " + last_name : first_name;

MessageBox.Show(result);

It causes a problem with Turkish characters like this:

enter image description here

How can I solve the problem?

question from:https://stackoverflow.com/questions/65870575/character-problem-while-getting-information-from-vcard-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please set encoding of file to utf-8. You can create a new file and save as utf-8, Otherwise you should get encoding of vCard file and tell the file reader.

For obtain encoding of the file you can use this method:

private static Encoding GetEncoding(string file)
{
    using (var reader = new StreamReader(file, Encoding.Default, true))
    {
        if (reader.Peek() >= 0)
            reader.Read();

        return reader.CurrentEncoding;
    }
}

and use it:

        var encoding = GetEncoding("yourfile");
        var lines = File.ReadAllLines("yourfile", encoding);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...