C#判斷某個字符串是否在另一個字符串數組中
阿新 • • 發佈:2018-04-11
bsp strong message blog 使用 AS 比較 默認 dialog 方法一:
需要用到的幾個方法
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
string t = "ttt";
return tt.Count(p=>p == t) > 0 //這個方法用來返回在指定得序列中滿足條件的元素數量 方法三: 使用 arr.Contains(str)方法,通過使用默認的相等比較器確定序列是否包含制定的元素。 以下是我工作編寫的代碼,用來打開一張圖片。把不符合要求的圖片篩選出來,防止報錯。
註意: 用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#判斷某個字符串是否在另一個字符串數組中