3. 巧截字符串的數字
輸入: A23BCDEFG4Hi678
輸出: 234678
Code
CharEnumerator CEnumerator = textBox1.Text.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 >= 48 && asciicode <= 57)
{
textBox2.Text += CEnumerator.Current.ToString();
}
}
4. 找出字符串中某一字符的所有位置
輸入: aBcdaEFGaHIaaaK, 查找字符: a
輸出: 0,4,8,11,12,13
Code
string str = textBox1.Text.Trim();
char[] myChar = str.ToCharArray();
for (int i = 0; i < myChar.Length; i++)
{
if (myChar[i].ToString() == textBox2.Text.Trim())
MessageBox.Show("字符串" + textBox2.Text.Trim() + "在" + textBox1.Text.Trim() + "中的位置為:" + i.ToString() + "\n");
}
5.從字符串分離文件路經, 文件名及擴展名
輸入: C:\gdiplus.dll
輸出: 路徑: C
文件名: gdiplus
擴展名:dll
Code
string strPath = textBox1.Text.Substring(0, textBox1.Text.LastIndexOf("\\"));
string strName=textBox1.Text.Substring(textBox1.Text.LastIndexOf("\\")+1,(textBox1.Text.LastIndexOf(".")-textBox1.Text.LastIndexOf("\\")-1) );
string strEName = textBox1.Text.Substring(textBox1.Text.LastIndexOf(".")+1, (textBox1.Text.Length - textBox1.Text.LastIndexOf(".")-1));
MessageBox.Show("文件路徑:"+strPath +"\n 文件名:"+strName +"\n 文件擴展名:"+strEName ,"信息",MessageBoxButtons.OK,MessageBoxIcon.Information );