//测试字符串 左包含 //string str = "AAABBBCCC"; //char[] ss = str.ToArray(); //0-8 字符数组 //char[] ss1 = str.ToCharArray();//0-8 字符数组 //给定一个字符串 AAA string str = "AAA"; char[] strs = str.ToArray(); string search = "AAsABBBCCC"; char[] searchs = search.ToCharArray(); int js = 0; for (int i = 0; i < searchs.Length; i++) { js++; if (i == strs.Length-1) //索引不能超过 (长度为3,索引0-2) { MessageBox.Show("包含"); //所有都相等,并且超出索引,证明包含 break; } if (searchs[i] != strs[i]) //在指定所用范围内只要有一个不相等, 证明不包含 { MessageBox.Show("不包含"); break; } }
string str = "AAA"; //要搜索的字符串 string search = "AAAsABBBCCC"; //要遍历的字符串 for (int i = 0; i < search.Length; i++) { if (i == str.Length) //索引不能超过 (长度为3,索引0-2) { MessageBox.Show("包含"); //所有都相等,并且超出索引,证明包含 break; } if (search[i] != str[i]) //在指定所用范围内只要有一个不相等, 证明不包含 { MessageBox.Show("不包含"); break; } }