关于查找字符的问题
比如有一文本有如下内容qwertiopghjklzbnm
现在的目的是想在这个文本中是否能查找到包含到用户输入的字串(用","号隔开,表示或的关系)如果用户输入 q,w 就表示用户想查找这个文本是否包含q或w,如果包含则返回true。
请大家给出代码,稍后我给了我的方法。
string value = "qwertiopghjklzbnm"; private string check() { string result = ""; List<char> inputValue = new List<char>(); inputValue.Add('q'); inputValue.Add('w'); int count = 0; List<char> list = value.ToList(); for (int i = 0; i < inputValue.Count; i++) { for (int j = 0; j < list.Count; j++) { if (inputValue[i].ToString().Trim() == list[j].ToString().Trim()) { count++; result += inputValue[i].ToString().Trim() + ","; if (count==inputValue.Count) { if (result.Length>0) { result = result.Substring(0, result.Length - 1); } } } } } return result; }
/******其实我不懂这个文件下载要怎么做,这是我模仿别人的代码写出来的,求改错*****/ /******Textbox1:下载链接,Textbox2:文件名,Textbox3:文件保存目录和文件名**********/ using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using using namespace FileDownload { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.textBox3.Text=@"C:\Documents and Settings\Administrator\桌面"; this.label5.Text = "准备就绪"; } private void btnOK_Click(object sender, EventArgs e) //开始下载 { this.btnOK.Enabled = false; this.label5.Text = "开始下载"; WebClient client = new WebClient(); string URL = this.textBox1.Text.Trim(); int n = URL.LastIndexOf('/'); string URLAddress = URL.Substring(0, n); string Dir = this.textBox3.Text.Trim(); string Path = Dir + "\\" + this.textBox2.Text.Trim(); if (Path.Contains(@"\\") == true) { Path.Replace(@"\\", @"\"); } try { WebRequest myre = WebRequest.Create(URLAddress); //为何程序每次运行到这里都出错了呢? try { client.DownloadFile(URLAddress, this.textBox2.Text.Trim()); Stream str = client.OpenRead(URLAddress); StreamReader sr = new StreamReader(str); byte[] mbyte = new byte[100000000]; //这个大小是不是限制了可以下载最大文件的大小? int allmybyte = (int)mbyte.Length; int startbyte = 0; while (allmybyte > 0) { int m = str.Read(mbyte, startbyte, allmybyte); if (m == 0) break; startbyte += m; allmybyte = m; } FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(mbyte, 0, startbyte); str.Close(); fstr.Close(); this.label5.Text = "下载完毕"; btnOK.Enabled = true; } catch (Exception o) { MessageBox.Show("连接失败:"+o.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); btnOK.Enabled = true; this.label5.Text = "连接失败"; } } catch(Exception o) { MessageBox.Show(o.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); btnOK.Enabled = true; this.label5.Text = "下载失败"; } } private void textBox1_TextChanged(object sender, EventArgs e) //下载链接更改 { string s = this.textBox1.Text.Trim(); //下载链接 if (s == string.Empty) { this.textBox2.Clear(); } else { int n = s.LastIndexOf('/'); string str = s.Substring(n + 1, s.Length - n - 1); this.textBox2.Text = str; //根据下载链接判断文件名 } } } }