-1

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

上下文:C#/Word

问题描述

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

我知道如果捆绑的字符串等于这个侧翼字符之一,就会检查checkbow &#120&#253并且&#254

查看机翼列表

否则复选框未选中。

在你的帮助下

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

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

但是当我创建一个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 !!");
            }
        }
    }
}

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

4

2 回答 2

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并进行比较。如果你想得到正确的排序规则,你必须指定很多其他的东西,比如文化等。但是,这是一个相当广泛的主题,不可能在这个答案中写下所有内容。

查看此msdn 页面以获取更多详细信息/选项string.compare

编辑- >更新后的问题:

假设您正在从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 && bom1 == 0x2f && bom2 == 0x76) return Encoding.UTF7;
    if (bom[0] == 0xef && bom1 == 0xbb && bom2 == 0xbf) return Encoding.UTF8;
    if (bom[0] == 0xff && bom1 == 0xfe) return Encoding.Unicode; //UTF-16LE
    if (bom[0] == 0xfe && bom1 == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE
    if (bom[0] == 0 && bom1 == 0 && bom2 == 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;
  }

代码片段取自这个stackoverflow 链接

于 2016-02-10T06:38:11.403 回答
0

那么当您比较字符串时,它将通过它们的值进行比较。

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

于 2016-02-10T05:32:18.797 回答