【问题标题】:Skip words / symbols when reading file读取文件时跳过单词/符号
【发布时间】:2013-03-13 21:35:15
【问题描述】:

我正在制作一个小型 C# 应用程序,但遇到了一个小问题。

我有一个纯文本的 .xml,我只需要第 4 行。

string filename = "file.xml";
if (File.Exists(filename))
{
    string[] lines = File.ReadAllLines(filename);
    textBox1.Text += (lines[4]);
}

到目前为止一切都很好,我唯一的问题是我必须从第 4 行删除一些单词和符号。

我的坏话和符号:

word 1 
: 
' 
, 

我一直在 google 上寻找,但是我找不到 C# 的任何东西。 找到了 VB 的代码,但我是新手,我真的不知道如何转换它并使其工作。

 Dim crlf$, badChars$, badChars2$, i, tt$
  crlf$ = Chr(13) & Chr(10)
  badChars$ = "\/:*?""<>|"           ' For Testing, no spaces
  badChars2$ = "\ / : * ? "" < > |"  ' For Display, has spaces

  ' Check for bad characters
For i = 1 To Len(tt$)
  If InStr(badChars$, Mid(tt$, i, 1)) <> 0 Then
    temp = MsgBox("A directory name may not contain any of the following" _
           & crlf$ & crlf$ & "     " & badChars2$, _
           vbOKOnly + vbCritical, _
           "Bad Characters")
    Exit Sub
  End If
Next i

谢谢。

已修复:)

 textBox1.Text += (lines[4]
              .Replace("Word 1", String.Empty)
            .Replace(":", String.Empty)
            .Replace("'", String.Empty)
            .Replace(",", String.Empty));

【问题讨论】:

  • 如果您的文件是 XML,您真的,真的应该将其解析为 XML。试试XDocument.Load
  • 检查 string.Replace ,谢谢。 --- XML 的内容取自 WEB,一个 JS 脚本。
  • 如果有帮助,您应该接受答案,而不仅仅是将问题标记为已解决...(它也会给您 5 个代表 :))

标签: c# vb.net symbols words


【解决方案1】:

你可以用任何东西替换它们:

textBox1.Text += lines[4].Replace("word 1 ", string.Empty)
                         .Replace(":", string.Empty)
                         .Replace("'", string.Empty)
                         .Replace(",", string.Empty);

或者也许创建一个你想要删除的表达式数组,然后将它们全部替换为空。

string[] wordsToBeRemoved = { "word 1", ":", "'", "," };

string result = lines[4];
foreach (string toBeRemoved in wordsToBeRemoved) {
    result = result.Replace(toBeRemoved, string.Empty);
}
textBox1.Text += result;

【讨论】:

  • 还有什么办法可以去掉空格吗?
  • @rgerculy 当然,只需将" " 添加到wordsToBeRemoved
【解决方案2】:

您可以使用String.Replace 将它们替换为空:

textBox1.Text += (lines[4]
            .Replace("Word 1", String.Empty)
            .Replace(":", String.Empty)
            .Replace("'", String.Empty)
            .Replace(",", String.Empty));

【讨论】:

    【解决方案3】:

    大家给出了很好的解决方案,我只想添加另一个快速(使用StringBuilder)和方便(使用扩展方法语法和params 作为值)的解决方案

    public static string RemoveStrings(this string str, params string[] strsToRemove)
    {
        var builder = new StringBuilder(str);
        strsToRemove.ToList().ForEach(v => builder.Replace(v, ""));
        return builder.ToString();
    }
    

    现在可以了

    string[] lines = File.ReadAllLines(filename);
    textBox1.Text += lines[4].RemoveStrings("word 1", ":", "'", ",");
    

    【讨论】:

    • 感谢您的回答,但我想要一些非常简单的东西也能理解我自己:)
    • @rgerculy 方法实现只有 3 行代码,你能简单到什么程度?
    • True ...但是第二行“ strsToRemove.ToList().ForEach(v => builder.Replace(v, "")); 让我很困惑。我是新人,我必须学习很多东西。
    • 好的,我明白了,总有一天,你会掌握LINQ的所有力量,我的儿子。让 Common Language Runtime 的力量与你同在,年轻的学徒。
    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    相关资源
    最近更新 更多