透過 CharEnumerator 逐字檢查 ASCII 碼 (48-57 為 '0'-'9'):
CharEnumerator ce = textBox1.Text.GetEnumerator();
while (ce.MoveNext())
{
byte[] arr = Encoding.ASCII.GetBytes(ce.Current.ToString());
int ascii = arr[0];
if (ascii >= 48 && ascii <= 57)
textBox2.Text += ce.Current.ToString();
}
// 輸入 "A23BCDEFG4Hi678" → 輸出 "234678"
string str = textBox1.Text.Trim();
char[] chars = str.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i].ToString() == textBox2.Text.Trim())
MessageBox.Show($"字元 '{textBox2.Text}' 在位置 {i}");
}
// 輸入 "aBcdaEFGaHIaaaK", 找 'a' → 0,4,8,11,12,13
string full = textBox1.Text; // 例: C:\gdiplus.dll
string path = full.Substring(0, full.LastIndexOf(@"\"));
string name = full.Substring(full.LastIndexOf(@"\") + 1,
full.LastIndexOf(".") - full.LastIndexOf(@"\") - 1);
string ext = full.Substring(full.LastIndexOf(".") + 1);
// 輸出: 路徑=C:, 檔名=gdiplus, 副檔名=dll
new string(input.Where(char.IsDigit).ToArray()) (LINQ 簡潔)。for (int i = str.IndexOf(c); i >= 0; i = str.IndexOf(c, i + 1)) 避免逐字轉陣列。System.IO.Path 類別 (GetDirectoryName, GetFileNameWithoutExtension, GetExtension) 跨平台相容。