开发技巧2

1、ASCII码值转换为字母

 

View Code
private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text == "")
{
MessageBox.Show(
"ASCII码值不能为空", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
}
else
{
byte[] array = new byte[1];
array[
0] = (byte)(Convert.ToInt32(textBox1.Text.Trim()));
textBox2.Text
= Convert.ToString(System.Text.Encoding.ASCII.GetString(array));
}
}
catch
{
MessageBox.Show(
"请输入合法的ASCII码值", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text
= "";
textBox1.Focus();
}
}

2去除字符串前后所有字符

View Code
/// <summary>
/// 去除字符串前后的所有空白字符
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string trim(string s)
{
Regex regexToTrim
= new Regex(@"(^\s*)|(\s*$)", RegexOptions.ECMAScript); // C#: 匹配任何空白字符, 与 [ \f\n\r\t\v] 等效
return regexToTrim.Replace(s, "");
}
posted @ 2011-03-01 18:59  笔墨伺候  阅读(147)  评论(0编辑  收藏  举报