【问题标题】:Replacing special characters in a list so you can search them easily替换列表中的特殊字符,以便您轻松搜索它们
【发布时间】:2014-10-04 14:10:57
【问题描述】:

我正在下载一个 xml 文件并将其保存到手机(Windows Phone 8 应用程序),然后我将其作为列表打开。此列表中包含带有大写、小写和特殊字符的希腊词。我希望用户能够通过此列表进行搜索,而无需让他编写与列表中相同的文本,因此,到目前为止,我将搜索栏和列表变成了大写字符,因此如果用户键入“to”该列表突出显示“to”、“To”、“TO”,暂时删除不匹配的单词,并显示匹配的单词。但是如果用户试图写一个没有语调的字母,那么列表中的有语调的单词将全部被删除,直到用户按下退格键并键入带有语调的字母。

示例:列表有 2 个单词。 Σπύρος , Μάρα。 用户按下字母“Μ”,在列表中,Σπύρος 消失,Μάρα 变为 Μάρα。现在用户输入“α”,Mάρα 也消失了,因为他必须输入“ά”。如果他有,那将是 Μάρα 等等。

所以我的问题是,是否可以将带有语调的字符替换为没有语调的相同字符?例如。 ά到a,Ά到A等。在Unicode字符表上,它们有不同的代码。

到目前为止的代码:

  private void businessFilterString_TextChanged(object sender, TextChangedEventArgs e)
    {
        sI.ItemsSource = businesses.Collection.Where(b => b.name.ToUpper().Contains(searchBox.Text.ToUpper()) || b.address.ToUpper().Contains(searchBox.Text.ToUpper())).ToList();
        LayoutUpdateFlag = true;
    }

    private void sI_LayoutUpdated(object sender, EventArgs e)
    {
        if (LayoutUpdateFlag)
        {
           SearchVisualTree(sI);
        }
        LayoutUpdateFlag = false;
    }
    private void SearchVisualTree(DependencyObject targetElement)
    {

        var count = VisualTreeHelper.GetChildrenCount(targetElement);

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(targetElement, i);
            if (child is TextBlock)
            {
                textBlock1 = (TextBlock)child;
                prevText = textBlock1.Text.ToUpper();
                HighlightText();
            }
            else
            {
                SearchVisualTree(child);
            }
        }
    }

  private void HighlightText()
  {
      if (textBlock1 != null)
      {
          string text = textBlock1.Text.ToUpper();
          textBlock1.Text = text;
          textBlock1.Inlines.Clear();

          int index = text.IndexOf(searchBox.Text.ToUpper());
          int lenth = searchBox.Text.Length;


          if (!(index < 0))
          {
              Run run = new Run() { Text = text.Substring(index, lenth), FontWeight = FontWeights.ExtraBold};
              run.Foreground = new SolidColorBrush(Colors.Orange);
              textBlock1.Inlines.Add(new Run() { Text = text.Substring(0, index), FontWeight = FontWeights.Normal});
              textBlock1.Inlines.Add(run);
              textBlock1.Inlines.Add(new Run() { Text = text.Substring(index + lenth), FontWeight = FontWeights.Normal });
          }
          else
          {
              //textBlock1.Text = "No Match";
          }
      }
  }

【问题讨论】:

标签: c# regex windows-phone-8 text


【解决方案1】:

所以我加了

  private static string DeleteAccentAndSpecialsChar(string OriginalText)
    {
        string strTemp = OriginalText;

        Regex rega = new Regex("[Ά]");
        Regex rege = new Regex("[Έ]");
        Regex regi = new Regex("[Ί]");
        Regex regu = new Regex("[Ύ]");
        Regex regh = new Regex("[Ή]");
        Regex rego = new Regex("[Ό]");
        Regex regw = new Regex("[Ώ]");

        strTemp = rega.Replace(strTemp, "Α");
        strTemp = rege.Replace(strTemp, "Ε");
        strTemp = regi.Replace(strTemp, "Ι");
        strTemp = regu.Replace(strTemp, "Υ");
        strTemp = regh.Replace(strTemp, "Η");
        strTemp = rego.Replace(strTemp, "Ο");
        strTemp = regw.Replace(strTemp, "Ω");

        return strTemp;
    }

改变了

   private void businessFilterString_TextChanged(object sender, TextChangedEventArgs e)
{
    sI.ItemsSource = businesses.Collection.Where(b => b.name.ToUpper().Contains(searchBox.Text.ToUpper()) || b.address.ToUpper().Contains(searchBox.Text.ToUpper())).ToList();
    LayoutUpdateFlag = true;
}

  private void businessFilterString_TextChanged(object sender, TextChangedEventArgs e)
    {
        sI.ItemsSource = businesses.Collection.Where(b => DeleteAccentAndSpecialsChar(b.name.ToUpper()).Contains(DeleteAccentAndSpecialsChar(searchBox.Text.ToUpper()))).ToList();
        LayoutUpdateFlag = true;
    }

这样所有的文字都显示在上面,而语调消失了。尽管如此,如果用户在他的文本上使用语调,则不会显示任何内容,但很适合我,因为上部文本不需要语调。

【讨论】:

    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2011-05-12
    • 2015-09-25
    相关资源
    最近更新 更多