什么是最有效的方式来放置字符串数组从.txt

本文关键字:字符串 数组 txt 有效 方式 什么 | 更新日期: 2023-09-27 18:04:01

我有一个大约有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

Error   2   Invalid expression term '=' c:'users'youtube upload'documents'visual studio 2013'Projects'Hangman'Hangman'Program.cs    16  35  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

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

什么是最有效的方式来放置字符串数组从.txt

从文件中获取值:

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'");                                   
  1. 变量声明不正确。正确的是

    string[] myWordArrays = ...

  2. ' @ '符号应该放在包含转义字符的字符串的正前面,并且应该在同一行。如果队伍太长怎么办?然后你可以把这个符号和字符串移到下一行。

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

  • 文件。ReadAllLines方法只接受完整路径,而不是相对路径。要指向位于存储执行文件的同一文件夹(bin文件夹)中的文件,可以使用Directory.GetCurrentDirectory()方法。可以改成:

    string filename = 1.txt;File.ReadAllLines(Directory.GetCurrentDirectory() + "'" + filename);

  • 根据用于分割字符串的字符,您可以使用以下字符之一:

            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()返回一个字符串。这个类有许多方法,定义在:https://msdn.microsoft.com/en-us/library/system.string (v = vs.110) . aspx

    这些方法包括一个"split"方法,该方法接受一个字符数组(或多个逗号分隔的字符)(用单引号(')表示的字符)或一个带有字符串拆分选项的字符串数组。

    在后者中,我们用new [] { "string1", "string2", "..."}定义了一个新的字符串数组,这里数组的类型是隐式的,但如果需要,可以指定new string[] {...},或者传递一个已经定义的字符串数组。第二个选项是带有两个值的enum;NoneRemoveEmptyEntries,定义如下:https://msdn.microsoft.com/en-us/library/system.stringsplitoptions (v = vs.110) . aspx

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

    我要感谢你们所有人的巨大帮助。我的格式不正确,但我想我明白它是如何工作的!!哈哈至少分号没有放错地方,对吧?:)

      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("WordList.txt");
            Console.WriteLine(myWordArrays[2]);
            Console.ReadLine();
    
          }
    
        }
    }
    
    非常感谢大家。这修复了它,它确实成功地显示了第三个元素(字符串),这是advice,恰好在第三行[2]-完美。谢谢你! !

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

    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;
        }
    }