C# 如何将Unicode字符串转换为另一种类型的Unicode字符串

C# 如何将Unicode字符串转换为另一种类型的Unicode字符串,c#,regex,unicode,pattern-matching,C#,Regex,Unicode,Pattern Matching,对于字符串中特定类型的Unicode字符串,我想用不同的类型替换Unicode字符串 (EX)1 (EX)2 我想知道如何用正则表达式进行模式匹配。 我想知道如何用这种方式替换它。我不熟悉C#(我主要使用Java),但下面是我将要做的一个抽象描述: (EX)1。 -将字符串转换为字符数组 创建一个空字符串(字符串s=”“) 创建一个循环,添加前缀和接下来的四个字符(在循环中:s=s+“&#x”+charArray[k]+charArray[k+1]+charArray[k+2]+charArr

对于字符串中特定类型的Unicode字符串,我想用不同的类型替换Unicode字符串

(EX)1

(EX)2

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

我不熟悉C#(我主要使用Java),但下面是我将要做的一个抽象描述:

(EX)1。 -将字符串转换为字符数组

  • 创建一个空字符串(字符串s=”“)

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

  • 在末尾加上分号

(EX)2

  • 您要匹配的模式将被替换为:“U\+”,您将用“&#x”替换该模式。但是,您必须事先取下第一个U+
必须在加号之前添加反斜杠的原因是,这是重复运算符,反斜杠将对其进行转义。我不知道C#,但在Java中,必须以字符串形式对转义进行转义,因此实际上要使用“U\\+”

Regex regexonicode=new Regex(@“U\+([0-9A-F]{4}+)+);
MatchCollection resultCollection=regexonicode.Matches(str);
foreach(在resultCollection中匹配){
int length=matched.Groups[0]。长度;
字符串matchedStr=matched.Groups[0].ToString();
int startIndex=str.IndexOf(matchedStr);
字符串temp=matchedStr;
字符串ret=“”;
字符串缓冲区=”;
int bufCount=0;
对于(int i=0;i
//Hexadecimal 4characters
string base="U+1234FFFF040001041234";
//I want to replace this type----> ሴЀĄሴ
//Hexadecimal 4characters
string base="U+1234 U+FFFF U+0400 U+0104 U+1234";
//----> ሴ  Ѐ Ą ሴ
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);                     
        }