6.批量替換某一類字符串
輸入: abcdsfjlsdkfjalsdkabcdefadslkfjlksdafabc
查找: abc
替換: ***
輸出: ***dsfjlsdkfjalsdk***defadslkfjlksdaf***
Code
public int M_int_index = -1;
private int M_int_start;
private int M_int_end;
M_int_index = 0;
while (M_int_index != -1)
{
M_int_start = 0;
M_int_end = richTextBox1.Text.Trim().Length;
M_int_index = richTextBox1.Find(this.textBox1.Text.Trim(), M_int_start, M_int_end, RichTextBoxFinds.None);
if (M_int_index == -1)
{
MessageBox.Show(this, "全部'" + this.textBox1.Text + "'已替換完畢。", "未找到",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
richTextBox1.SelectedText = textBox2.Text;
M_int_index += this.textBox1.Text.Length;
}
}
7.把一個按空格分割的字符串存儲在一個數組中 (此處測試用ArrayList)
輸入: abc def ghiklm opq
輸出: 可按數組下標輸出: 如 arr[1]=def
Code
string str = "abc def ghiklm opq";
string[] strArr = str.Split(' ');
System.Collections.ArrayList mylist = new System.Collections.ArrayList();
foreach (string strArray in strArr)
{
mylist.Add(strArray);
}
listBox1.DataSource = mylist;