C#判斷某個字串是否在另一個字串數組中

來源:互聯網
上載者:User

標籤: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#判斷某個字串是否在另一個字串數組中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.