【问题标题】:How to Unicode String Convert to another type of Unicode String如何将 Unicode 字符串转换为另一种类型的 Unicode 字符串
【发布时间】:2017-03-24 01:46:29
【问题描述】:

当涉及到字符串中特定类型的 Unicode 字符串时,我想将 Unicode 字符串替换为不同的类型。

EX) 1.

//Hexadecimal 4characters
string base="U+1234FFFF040001041234";
//I want to replace this type----> ሴЀĄሴ

前)2.

//Hexadecimal 4characters
string base="U+1234 U+FFFF U+0400 U+0104 U+1234";
//----> ሴ  Ѐ Ą ሴ

我想知道如何使用正则表达式进行模式匹配。 我想知道如何以这种方式替换它。

【问题讨论】:

    标签: c# regex unicode pattern-matching


    【解决方案1】:

    我不熟悉 C#(我主要使用 Java),但这里是对我将做什么的抽象描述:

    EX) 1. - 将字符串转成字符数组

    • 创建一个空字符串(String s = "")

    • 创建一个循环,添加前缀和接下来的四个字符(在循环中:s = s + "" + charArray[k] + charArray[k+1] + charArray[k+2 ] + charArray[k+3])

    • 在末尾添加分号

    前)2.

    • 您要匹配的将被替换的模式是:“U\+”,您将用“; ”替换它。但您必须事先取下第一个 U+。

    之所以要在加号前加反斜杠,是因为那是重复操作符,反斜杠会转义。我不了解 C#,但在 Java 中,您必须在字符串中转义转义,因此您实际上会使用“U\\+”

    【讨论】:

      【解决方案2】:
      Regex regexUnicode = new Regex(@"U\+([0-9A-F]{4})+");
      
              MatchCollection resultCollection = regexUnicode.Matches(str);
              foreach (Match matched in resultCollection) {
      
                  int length = matched.Groups[0].Length;                      
                  string matchedStr = matched.Groups[0].ToString();           
                  int startIndex = str.IndexOf(matchedStr);                   
                  string temp = matchedStr;
                  string ret = "";
                  string buffer = "";
                  int bufCount = 0;
                  for (int i = 0; i < matchedStr.Length; ++i) {
                      if (matchedStr[i] == 'U' || matchedStr[i] == '+') {
                          continue;
                      } else if (bufCount != 4) {                         
                          buffer += matchedStr[i];
                          bufCount++;
                      } else if (bufCount == 4) {                          
                          ret += "&#x" + buffer + ";";
                          buffer = "";
                          buffer += matchedStr[i];
                          bufCount = 1;
                      }
                  }
                  ret += "&#x" + buffer + ";";
                  str = str.Remove(startIndex, matchedStr.Length);       
                  str = str.Insert(startIndex, ret);                     
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        • 2017-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多