How to determine text format in C#

烂漫一生 提交于 2019-12-06 04:11:55

问题


Is there a way to determine the format of text in C#/.NET

something like this would be very useful.

public TextFormat TextTools.GetTextFormat(string text);

switch(TextTools.GetTextFormat(mystring))
{

  case TextFormat.RichText: break;
  case TextFormat.PlainText: break;

}

Ive looked around on msdn but couldnt find such a tool


回答1:


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;
}


来源:https://stackoverflow.com/questions/22502924/how-to-determine-text-format-in-c-sharp

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