標籤:extension path 直接 bool 防止 sage result div 比較
方法一:需要用到的幾個方法string.Split(char);//按照char進行拆分,返回字串數組Array.IndexOf(Array,string):返回指定string在array中的第一個匹配項的下標Array.LastIndexOf(Array,string):返回指定string在array中的最後一個匹配項的下標如果沒有匹配項,則返回-1[範例程式碼]:string strNum="001,003,005,008";string[] strArray=strNum.Split(‘,‘);//按逗號拆分,拆分字元為char或char數組Console.WriteLine(Array.IndexOf(strArray,"004").ToString());引自:https://www.cnblogs.com/yliang/archive/2013/04/30/3052100.html
注意: 用IList需要using System.Collections;
數組中不能直接indexOf這個方法,需要 Array.IndexOf(x,x);這樣來使用
方法二:string[] tt = new string[] {"abc","ttt","efg" };
string t = "ttt";
return tt.Count(p=>p == t) > 0 //這個方法用來返回在指定得序列中滿足條件的元素數量 方法三:使用 arr.Contains(str)方法,通過使用預設的相等比較子確定序列是否包含制定的元素。以下是我工作編寫的代碼,用來開啟一張圖片。把不符合要求的圖片篩選出來,防止報錯。
OpenFileDialog dialog = new OpenFileDialog();
string[] mys = { ".bmp", ".jpg", ".png", ".gif", ".ico" };
if (dialog.ShowDialog() == DialogResult.OK)
{
string fileType=Path.GetExtension(dialog.FileName);
bool a= mys.Contains(fileType);
if (a)
{
Bitmap p1 = new Bitmap(dialog.FileName);
pictureBox1.Image = p1;
}
else
{
MessageBox.Show("暫不支援該圖片格式!");
}
}
C#判斷某個字串是否在另一個字串數組中