在一般的字符串中,有用“ ”“,”组成的,在一般情况下,需要进行单个提取,使用的Split()函数,实例如下。
//打开要读取的文件,获得文件路径
private void Script_Log_Select_Click(object sender, EventArgs e)
{
OpenFileDialog Openfile = new OpenFileDialog();
Openfile.Filter = "txt File|*.txt";
//Add format
Openfile.AddExtension = true;
//Set the title
Openfile.Title = "Script File";
if (Openfile.ShowDialog() == DialogResult.OK)
{
ScriptFilePath = Openfile.FileName;
if (!File.Exists(ScriptFilePath))
{
MessageBox.Show(" File not exits !");
return;
}
// Script_Log_File.Text = ScriptFilePath.ToString();
Script_Log_File.Text=(Path.GetDirectoryName(ScriptFilePath)+"\\"+ Path.GetFileNameWithoutExtension(ScriptFilePath)).ToString();
StreamReader SR1 = new StreamReader(ScriptFilePath, Encoding.Default);
while (!SR1.EndOfStream)
{
string LineString;
LineString = SR1.ReadLine() + "\r\n";
fun(LineString, Script);
}
SR1.Close();
}
}
//获取文本一行,按特殊字符切割字符串
private void Script_Execute_Click(object sender, EventArgs e)
{
Script.Clear();
if (!File.Exists(ScriptFilePath))
{
MessageBox.Show(" File not exits !");
return;
}
StreamReader SR1 = new StreamReader(ScriptFilePath, Encoding.Default);
string LineString;
LineString = SR1.ReadLine();
string[] StringArray = LineString.Split(' '); //按照“ ”进行切割
StringArray[0]...
StringArray[1]...
... ...
}