让我们说那是文件中的文字.它会删除冒号,并将每个单词放入数组中自己的字符串中.例如:
exampleArray[0] = 'hello'
exampleArray[1] = 'my'
exampleArray[2] = 'name'
exampleArray[3] = 'is'
exampleArray[4] = 'lavi'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text Files|*.txt";
DialogResult result = ofd.ShowDialog();
if(result == DialogResult.OK)
{
StreamReader textfile = new StreamReader(ofd.FileName);
string s = textfile.ReadToEnd();
string[] split = s.Split(':', '\n');
foreach (string word in split)
textBox1.Text = word[0].ToString();
//listBox1.Items.Add(word);
ofd.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:我想说的是如何制作它,以便每个单词存储在一个数组中,以便稍后用[0],[1],[2]等访问它?如果Split自动执行此操作,如何访问每个单词?
它自动完成(String.split,即)
String str = "hello:my:name:is:lavi";
var words = str.Split(":");
Console.WriteLine(words[1]); //This prints out 'my';
for (int i=0;i<words.Length;i++) { //This will print out each word on a separate line
Console.WriteLine(words[i]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16671 次 |
| 最近记录: |