用RichTextBox实现字符串中指定字符(汉字)的颜色改变
private void Form1_Load(object sender, EventArgs e)
{
string s = "aaa。bbb‘’中a国";
ArrayList arr = Validation(s);
richTextBox1.Text = s;
for (int i = 0; i < arr.Count; i++)
{
richTextBox1.Select((int)arr[i], 1);
richTextBox1.SelectionColor = Color.Red;
}
}
private int GetCharASCII(char c)
{
return (int)c;
}
private ArrayList Validation(string s)
{
char[] charArray = s.ToCharArray();
ArrayList intArr = new ArrayList();
for (int i = 0; i < s.Length; i++)
{
char c = charArray[i];
if (GetCharASCII(c) > 127)
{
intArr.Add(i);
}
}
return intArr;
}