【问题标题】:Split string from text file从文本文件中拆分字符串
【发布时间】:2015-03-18 15:38:27
【问题描述】:

我正在尝试将字符串从文本文件转换为键,我需要拆分文本。 例如: 代码c#

 string[] controls = File.ReadAllLines(FilePath);
 Keys move up = (Keys)Enum.Parse(type of(Keys),controls[1].Split("|", StringSplitOption.None), true);

在 [1] 行的文本文件中,我有: 上移 |W;

我想将字符 W 设置为键。

感谢您的回复,如果我的英语看起来很奇怪,请见谅。

【问题讨论】:

  • 感谢回复,但我知道索引值。我试过你写的位或不起作用,无法编译。 (无法将“string”转换为“string[]”)所以我创建了另一个字符串数组,但出现了更多错误。

标签: c# string split system.io.file


【解决方案1】:

如果您对 | 之后的字符串感兴趣,那么应该是:

controls[1].Split("|", StringSplitOption.None)

替换为:

controls[1].Split("|")[1]

[1] 表示从Split()创建的数组中返回第二个索引值

如果您尝试从第 1 行获取,那么 controls[1] 应该是 controls[0],因为数组是基于零索引的。

【讨论】:

  • 您的解决方案没有完美运行,但它帮助我解决了,将 controls[1].Split("|", StringSplitOption.None)[1] 替换为 string test = controls.Split( '|')[1];
  • @Newokmyne 编辑了我的帖子,因此可以帮助其他人来此帖子
猜你喜欢
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
相关资源
最近更新 更多