【问题标题】:C#/Word : How to compare two strings with wingdings characters?C#/Word:如何将两个字符串与wingdings字符进行比较?
【发布时间】:2016-02-10 04:03:08
【问题描述】:

我想将两个字符串与wingdings 字符进行比较。因为我不知道如何在 C# 字符串中捕获wingdings 代码,所以我无法在两个字符串之间进行可靠的比较...

上下文:C#/Word

问题描述

我有一个带有复选框的 word 文档。此复选框是一个侧翼。我的挑战是说是否使用 C# 选中了此复选框。

我知道如果 hundled 字符串等于此翼字符 &#120&#253&#254 之一,则会检查检查弓

see the list of wingdings

否则复选框未选中。

在您的帮助下

在您的帮助下,我的问题有了解决方案:),现在,我有另一个问题:))

正如我在顶部所说,所有这一切都是为了将​​侧翼字符(复选框)与代表选中复选框的所有可能的侧翼进行比较。现在,当我直接将单词中的复选框与编码的侧翼进行比较时,我找不到字符,因为它是以其他格式编码的......

但是当我创建一个 word 文档并输入编码字符然后我将创建的字符与目标 word doc 进行比较时,我有我想要的 :) 但是你看到的这个解决方案需要创建一个新的 word doc :(

无论如何,这是我的解决方案:)

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace Project0
{
    class Program
    {
        static bool isChecked(Range rng)
        {
            var wrdApp = new Application();
            wrdApp.Visible = true;
            Document wrdDoc = wrdApp.Documents.Add();
            Range Rngg = wrdDoc.Paragraphs[1].Range;
            Rngg.Font.Name = "WingDings";   // Why I CREATE A SECOND DOC - I Need this font ;(
            List<string> list = new List<string>{ "\u00fe", "\u00fd", "\u0078" };
            foreach (string k in list)
            {
                if (rng.Text.Contains(Rngg.Text))
                {
                    return true;
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
            List<string> list = new List<string> { "\u00fe", "\u00fd", "\u0078" };

            //            string wPath = @"C:\DV\test.docx";
            //           Document wrdDoc = wrdApp.Documents.Open(FileName: wPath, ReadOnly: false);

            var wrdApp = new Application();
            wrdApp.Visible = true;
            Document wrdDoc = wrdApp.Documents.Add();

            // I SIMULATE MY DOC
            wrdDoc.Paragraphs[1].Range.Font.Name = "WingDings";
            wrdDoc.Paragraphs[1].Range.Text = list[1];  // I put a checked box

            // CAN I DETERMINE IF THERE IS A CHECKBOX
            if (isChecked(wrdDoc.Paragraphs[1].Range))
            {
                Console.WriteLine("Heeah, there's a checked checkbox !!");
            }
        }
    }
}

提前感谢您的所有帮助:)

【问题讨论】:

    标签: c# string ms-word compare wingdings


    【解决方案1】:

    当你声明一个字符串为

    string x="?♓■♑⬧♎♓■♑⬧";  //Wingdings characters
    

    .NET 将该字符串保存为 UTF-16 编码的 unicode 代码点

    因此,您使用什么字体或符号 来声明string 并不重要。即使您的机器上没有要显示的正确字体文件,您仍然可以使用\u 表示法保存该字体值。这意味着直接编写 unicode 代码点:

    类似:

    string test= "\u20AC";
    //Also can be declared as: 
    string test=     "€" 
    //Both are same
    

    因此,您仍然可以将任何“字符”“保存”到您的string 并进行比较。如果你想获得正确的排序顺序,你必须指定很多其他的东西,比如 culture 等。 但是,这是一个相当广泛的主题,不可能在这个答案中写下所有内容。

    查看msdn page 了解更多关于string.compare 的详细信息/选项

    EDIT -> 更新后的问题:

    假设您正在读取来自 UTF-8 编码源的值,您可以执行以下操作:

    string yourSourceStringInUTF8="asdfasdýadfasdfasdf";
    //These are the wingdings characters in UTF-8 encoded hex format 
    List<string> list = new List<string> { "\u00fe", "\u00fd", "\u0078" };
    bool found = false;
    foreach (string s in list)
    {
        found = yourSourceStringInUTF8.Contains(s);
        if (found)
        {
            break;
        }
    }
    Console.WriteLine(found? "found":"Notfound");
    

    编辑

    有多种方法可以获取/尝试查找文件的编码。 有些是:

    
    public static Encoding GetEncoding(string filename)
    {
        // Read the BOM
        var bom = new byte[4];
        using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read))
        {
            file.Read(bom, 0, 4);
        }
    // Analyze the BOM
        if (bom[0] == 0x2b && bom@987654322@ == 0x2f && bom@987654323@ == 0x76) return Encoding.UTF7;
        if (bom[0] == 0xef && bom@987654324@ == 0xbb && bom@987654325@ == 0xbf) return Encoding.UTF8;
        if (bom[0] == 0xff && bom@987654326@ == 0xfe) return Encoding.Unicode; //UTF-16LE
        if (bom[0] == 0xfe && bom@987654327@ == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE
        if (bom[0] == 0 && bom@987654328@ == 0 && bom@987654329@ == 0xfe && bom[3] == 0xff) return Encoding.UTF32;
        return Encoding.ASCII;
    }
    
    

    
          using (StreamReader reader = new StreamReader(fileName, defaultEncodingIfNoBom, true))
      {
          reader.Peek(); // you need this!
          var encoding = reader.CurrentEncoding;
      }
    

    代码sn-p取自stackoverflow link

    【讨论】:

    • 朋友们好,我刚刚发布了我的问题的详细信息:)
    • 感谢 ANewGuyInTown !!!我更新了问题:)这个问题没有尽头:)。 (我的最后一个代码需要 MS Word / Visual Studio)
    • 如果您不知道编码,您无能为力。这一直是internet 本身的一个主要问题。如果文件没有任何编码元数据,那么你所能做的就是猜测。尽管如此,还是有一些聪明的猜测可以在相当长的时间内得到正确的
    • 再次感谢 ANewGuyInTown !!!老实说,我不明白我怎么能用你的答案来解决我的问题:)即使你的代码有效:)
    • 假设您不确定文件的编码,这就是问题所在,我可以建议:使用方法GetEncoding 查找文件的编码,然后创建@987654341 @,它将上一次调用返回的encoding 作为parameter 并返回包含编码wingdings charactersList&lt;string&gt;,(您必须找出从上面GetEncoding 方法返回的每个编码的这些字符示例仅适用于UTF-8) 然后使用返回的List&lt;string&gt; 来比较您的文件。 (如第一个示例)
    【解决方案2】:

    当你比较字符串时,它会根据它们的值进行比较。

    因此,您可以将带有绕组的字符串放在字符串变量中,然后使用 string.comapare() 方法对其进行比较。 但是,我认为您应该详细说明您的问题,然后会有答案。

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 2020-07-01
      • 2016-10-17
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2021-08-25
      相关资源
      最近更新 更多