Convert TXT File of Unknown Encoding to String

前端 未结 1 929
一生所求
一生所求 2021-01-19 17:43

How can I convert Plain Text (.txt) files to a string if the encoding type is unknown?

I\'m working on a feature that would allow users to import txt files into my a

相关标签:
1条回答
  • 2021-01-19 18:02

    Sometimes stringWithContentsOfFile:usedEncoding:error: can do the job (esp if the file has a Byte Order Mark):

    NSError *error;
    NSStringEncoding encoding;
    NSString *string = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:&error];
    

    Note, this rendition with usedEncoding should not be confused with the similarly named method that just has a encoding parameter.

    0 讨论(0)
提交回复
热议问题