C# 从.txt将字符串放入数组的最有效方法是什么

C# 从.txt将字符串放入数组的最有效方法是什么,c#,arrays,string,text,pc,C#,Arrays,String,Text,Pc,我有一个文本文件,大约有30个单词(长度不同)。我正试图将所有这些单词放入程序中,并将它们存储到一个“x”元素数组中(x是单词数) 有什么帮助吗?我在学习的第二天 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hangman { class Prog

我有一个文本文件,大约有30个单词(长度不同)。我正试图将所有这些单词放入程序中,并将它们存储到一个“x”元素数组中(x是单词数)

有什么帮助吗?我在学习的第二天

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hangman {
    class Program {
        static void Main(string[] args) {
            //StreamReader myWordList = new  StreamReader("WordList.txt");//string stringArray[] = StreamReader(WordList.txt);//.WordList.txt;

            String myWordArrays[] = File.ReadAllLines(@
            "C:\Users\YouTube Upload\Documents\Visual Studio 2013\Projects\Hangman");

            Console.WriteLine(myWordArrays[2]);
            Console.ReadLine();
        }
    }
}
这是完整的代码(如上)-我收到以下错误:

Error   1   Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.  c:\users\youtube upload\documents\visual studio 2013\Projects\Hangman\Hangman\Program.cs    16  32  Hangman

我真的不明白这一点,因为我称之为我应该的(或者我会这么认为?o.0)

这是:

Error   3   ; expected  c:\users\youtube upload\documents\visual studio 2013\Projects\Hangman\Hangman\Program.cs    16  35  Hangman
还有这个

Error   4   ; expected  c:\users\youtube upload\documents\visual studio 2013\Projects\Hangman\Hangman\Program.cs    16  37  Hangman

请原谅我在这里糟糕的格式设置-我是这个网站的新手,刚刚习惯了这一切(

试试这个,它可能会对你有所帮助

private void PutTextIntoArray()
{
    var array = ReadTextFile("C:\\WordList.txt").GetTotalWords();
}    

private static string ReadTextFile(string file)
{
    try
    {
        return File.ReadAllText(file);
    }
    catch (IOException ex)
    {
        return ex.Message;
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}
public static class Extensions
{
    public static string ReplaceMultipleSpacesWith(this string text, string newString)
    {
        return Regex.Replace(text, @"\s+", newString);
    }

    public static string[] GetTotalWords(this string text)
    {
        text = text.Trim().ReplaceMultipleSpacesWith(" ");
        var words = text.Split(' ');
        return words;
    }
}
根据用于拆分字符串的字符,您可以使用以下选项之一:

        var streamReader = new StreamReader("file");
        var words = streamReader.ReadToEnd();

        var wordArray = words.Split(new[] { Environment.NewLine }, StringSplitOptions.None); // For words split by lines where you know the format of the line ending
        var wordArray = words.Split(new [] {"\n", "\r\n"}, StringSplitOptions.None); // For words split by lines where the format could be unix or windows
        var wordArray = words.Split(','); // For words split by comma
因此,有关详细说明,请参见
StreamReader.ReadToEnd()
返回一个字符串。此类有许多在中定义的方法:

这些方法包括“split”方法,该方法采用字符数组(或多个逗号分隔字符)(字符由单引号(')表示)或带有字符串拆分选项的字符串数组

在后者中,我们使用
new[]{“string1”、“string2”、““…”}
定义一个新的字符串数组,这里数组的类型是隐式的,但是我们可以指定
new string[]{…}
如果需要,或传入已定义的字符串数组。第二个选项是具有两个值的枚举;
None
removemptyEntries
,它们在此处定义:


string.split
方法(在顶部链接中)还有额外的重载。

我想感谢大家的巨大帮助。我的格式不正确,但我想我理解它到底是如何工作的!!哈哈。至少它没有放错/丢失分号,是吗?:)


非常感谢大家。这修复了它,并且它成功地显示了第三个元素(字符串),这是advice,正好在第三行[2]-完美。谢谢你

像这样从文件中获取值:

string[] myWordArrays = File.ReadAllLines(@"C:\Users\YouTube Upload\Documents\Visual Studio 2013\Projects\Hangman\yourfilename");
你的整个计划:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hangman {
    class Program {
        static void Main(string[] args) {
            string[] myWordArrays = File.ReadAllLines(@"C:\Users\YouTube Upload\Documents\Visual Studio 2013\Projects\Hangman\yourfilename");

            Console.WriteLine(myWordArrays[2]);
            Console.ReadKey();
        }
    }
}
另外,你在这行代码中犯了什么错误

String myWordArrays[] = File.ReadAllLines(@
            "C:\Users\YouTube Upload\Documents\Visual Studio 2013\Projects\Hangman\");                                   
  • 变量声明不正确。正确的一个是

    string[]myWordArrays=…

  • @
    ”符号应该放在包含转义字符的字符串的正前方,并且应该在同一行上。如果线路太长怎么办?然后可以将此符号和字符串移动到下一行

  • string[]myWordArrays=File.ReadAllLines(
    @“C:\Users\YouTube Upload\Documents\Visual Studio 2013\Projects\Hangman”)

  • File.ReadAllLines方法只接受完整路径,不接受相对路径。要指向存储执行文件的同一文件夹(bin文件夹)中的文件,可以使用Directory.GetCurrentDirectory()方法。因此,您可以将其更改为:

    字符串文件名=1.txt; File.ReadAllLines(Directory.GetCurrentDirectory()+“\”+文件名)


  • 单词的数量总是30个,还是可以变化?是用行分隔还是用空格分隔?如果是前者,
    System.IO.File.ReadAllLines(“WordList.txt”)将为您提供一个字符串数组。如果是后者,则使用诸如
    File.ReadAllText(“WordList.txt”).Split(“”)等基本内容
    将为您提供一个文件内容字符串,用空格分割,得到一个字符串数组。@新手您刚刚发布的编辑功能可能有一个答案,是否有问题?如果不是的话,你最好离开原来的问题,然后发布你自己的问题答案(这在这个网站上是允许的,甚至是鼓励的)。@William-是的,它可能会有所不同。我刚去了一个网站,上面有一个随机单词的列表,并进行了复制/粘贴,每个单词都在单独的行上。我已经用新代码更新了我的帖子。我之前在那里的东西乱七八糟。它仍然不起作用,现在给了我各种各样的错误。我试着使用File.ReadAllLines(“WordList.txt”);但这不起作用,所以我指定了,但仍然不起作用(您的数组变量声明不正确,我以前没有发现。应该是这样:
    string[]myWordsArray=File.ReadAllLines…
    not
    string myWordsArray[]
    。这个问题可能会导致其他问题层出不穷,因为据我所知,其他问题在语法上看起来很好。谢谢你。这看起来比我现在坐的位置要先进得多,但我正在接受这一切,这肯定会给我未来的参考点!这看起来很疯狂!哈哈。有很多事情我以前没见过这里,但是现在接触到它很好,因为我要把它输入到VSE中,看看发生了什么事!谢谢!我为你的好处添加了更多的解释看看这里关于ReadAllLines方法的信息:你可能想添加一些异常处理哦,太好了。谢谢你,威廉。这是一个很好的参考。Y是的,我在发布这篇文章后立即修复了它。我非常感谢。:)我只是从项目中引用了它。现在,我很好奇——我应该只引用文件名(“WordList.txt”)还是总是将它指向正确的文件夹?你认为哪一个最好?另外,我的显示器是42”-它对我来说并不太长。哈哈。当我为这个网站的目的格式化它时,我最后不得不点击“回车”,然后再点击“回车”,这样我就可以缩进行,使它显示为“代码”。这就是我为什么留下它的原因。哈哈,非常感谢,Fab。好吧,我已经回答了我自己的问题(上面)简单地做了一个File.ReadAllLines(“WordList.txt”);它工作得很好。或者你指的是不同的东西?
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Hangman {
        class Program {
            static void Main(string[] args) {
                string[] myWordArrays = File.ReadAllLines(@"C:\Users\YouTube Upload\Documents\Visual Studio 2013\Projects\Hangman\yourfilename");
    
                Console.WriteLine(myWordArrays[2]);
                Console.ReadKey();
            }
        }
    }
    
    String myWordArrays[] = File.ReadAllLines(@
                "C:\Users\YouTube Upload\Documents\Visual Studio 2013\Projects\Hangman\");