在短信平台的项目中有遇到取出手机发送上来的信息中的命令字,即取出字符串中从第一位开始到字母到遇见第一次不是字母结束的字符。
主要是根据字母的ASCII进行判断,如果第一次出现不在字母的ASCII范围内就跳出即可.有如下代码:
Code
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(GetCommandCode(this.textBox1.Text.Trim()));
}
private string GetCommandCode(string input)
{
string str = string.Empty;
CharEnumerator CEnumerator = input.GetEnumerator();
while (CEnumerator.MoveNext())
{
byte[] array = new byte[1];
array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
int asciicode = (short)(array[0]);
if ((asciicode >= 97 && asciicode <= 122) || (asciicode >= 65 && asciicode <= 90))
{
str += CEnumerator.Current.ToString();
}
else
break;
}
return str;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(GetCommandCode(this.textBox1.Text.Trim()));
}
private string GetCommandCode(string input)
{
string str = string.Empty;
CharEnumerator CEnumerator = input.GetEnumerator();
while (CEnumerator.MoveNext())
{
byte[] array = new byte[1];
array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
int asciicode = (short)(array[0]);
if ((asciicode >= 97 && asciicode <= 122) || (asciicode >= 65 && asciicode <= 90))
{
str += CEnumerator.Current.ToString();
}
else
break;
}
return str;
}