Imports System.Text.RegularExpressions
------------------------------------------
Sub Unicode2Character()
Dim doc As Document = DTE.ActiveDocument
Dim docText As TextDocument = doc.Object
Dim selText As TextSelection = docText.Selection()
selText.SelectAll()
Dim text As String = selText.Text
Dim iLength As Integer
Do
iLength = text.Length
Dim m As Match
Dim strPattern As String = "(?<code>\\u[A-F0-9]{4})"
m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
If m.Success Then
Dim strValue As String
strValue = m.Groups("code").Value
text = text.Replace(strValue, "")
Dim int As Integer
int = System.Int32.Parse(strValue.Substring(2, 4), NumberStyles.HexNumber)
Dim ch As Char = ChrW(int)
docText.ReplacePattern(strValue, ch)
Else
Exit Do
End If
If Not text.Length < iLength Then
Exit Do
End If
Loop
selText.StartOfDocument()
End Sub
------------------------------------------
Sub Unicode2Character()
Dim doc As Document = DTE.ActiveDocument
Dim docText As TextDocument = doc.Object
Dim selText As TextSelection = docText.Selection()
selText.SelectAll()
Dim text As String = selText.Text
Dim iLength As Integer
Do
iLength = text.Length
Dim m As Match
Dim strPattern As String = "(?<code>\\u[A-F0-9]{4})"
m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
If m.Success Then
Dim strValue As String
strValue = m.Groups("code").Value
text = text.Replace(strValue, "")
Dim int As Integer
int = System.Int32.Parse(strValue.Substring(2, 4), NumberStyles.HexNumber)
Dim ch As Char = ChrW(int)
docText.ReplacePattern(strValue, ch)
Else
Exit Do
End If
If Not text.Length < iLength Then
Exit Do
End If
Loop
selText.StartOfDocument()
End Sub
到上面这个网试试
void Unicode2Character()
{
Document doc = DTE.ActiveDocument;
TextDocument docText = doc.Object;
TextSelection selText = docText.Selection();
selText.SelectAll();
string text = selText.Text;
int iLength;
do {
iLength = text.Length;
Match m;
string strPattern = "(?<code>\\\\u[A-F0-9]{4})";
m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase);
if (m.Success) {
string strValue;
strValue = m.Groups("code").Value;
text = text.Replace(strValue, "");
int int;
int = System.Int32.Parse(strValue.Substring(2, 4), NumberStyles.HexNumber);
char ch = ChrW(int);
docText.ReplacePattern(strValue, ch);
} else {
goto exitDoLoopStatement0;
}
if (!(text.Length < iLength)) {
goto exitDoLoopStatement1;
}
} while (true);
exitDoLoopStatement1: ;
selText.StartOfDocument();
}
这个在C#是退出循环的意思吗?我刚学C#
{
Document doc = DTE.ActiveDocument;
TextDocument docText = doc.Object;
TextSelection selText = docText.Selection();
selText.SelectAll();
string text = selText.Text;
int iLength;
while(true)
{
iLength = text.Length;
Match m;
string strPattern = @"(?<code>\\u[A-F0-9]{4})";
m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase);
if(m.Success)
{
string strValue = m.Groups["code"].Value;
text = text.Replace(strValue, "");
int intt = System.Int32.Parse(strValue.Substring(2, 4), NumberStyles.HexNumber);
char ch = (char)intt;
docText.ReplacePattern(strValue, ch);
}
else
break;
if(!(text.Length < iLength))
break;
}
selText.StartOfDocument();
}
{
Document doc = DTE.ActiveDocument;
TextDocument docText = doc.Object;
TextSelection selText = docText.Selection();
selText.SelectAll();
string text = selText.Text;
int iLength;
while(true)
{
iLength = text.Length;
Match m;
string strPattern = "(?<code>\\u[A-F0-9]{4})";
m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase);
if(m.Success)
{
string strValue;
strValue = m.Groups("code").Value;
text = text.Replace(strValue, "");
int int1;
int1 = System.Int32.Parse(strValue.Substring(2, 4), NumberStyles.HexNumber);
char ch = (char)int1;
docText.ReplacePattern(strValue, ch);
}
else
break;
if(! text.Length < iLength)
break;
}
selText.StartOfDocument();
}其中用到的对象要自己添加引用
我只要输出个函数就行了。
Function Unicode2Character(String):String;//替换\u1234到汉字
Begin
end;