How to determine text format in C#

╄→尐↘猪︶ㄣ 提交于 2019-12-04 09:45:04
Adriano Repetti

It's a pretty heuristic check but you can try to build your own function starting with something like this (of course you can extend it to handle different formats):

public static TextFormat GetFormat(string text) {
    if (text.TrimStart().StartsWith(@"{\rtf1", StringComparison.Ordinal))
        return TextFormat.RichText;

    return TextFormat.PlainText;
}

A better check implies you parse RTF text to be sure it's not just a random string that seems RTF. Because parsing may be expansive (in terms of time) then I'd suggest to first do a quick check to exclude everything for sure isn't RTF:

public static TextFormat GetFormat(string text) {
    if (text.TrimStart().StartsWith(@"{\rtf1", StringComparison.Ordinal)) {
        if (IsValidRtf(text))
            return TextFormat.RichText;
    }

    return TextFormat.PlainText;
}

In the most nested if you can decide what to do with text that seems RTF but it's not valid (in this example I just consider it as plain text). A possible, naive and inefficient, implementation of IsValidRtf() that relies on RichTextBox control implementation (then down to Windows API implementation) may be:

private static bool IsValidRtf(string text) {
    try {
        new RichTextBox().Rtf = text;
    }
    catch (ArgumentException) {
        return false;
    }

    return true;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!