簡體   English   中英

需要幫助閱讀一些 C#

[英]Need Help Reading Some C#

代碼是:

 private static void loadAccounts()
 {
    using (TextReader tr = new StreamReader("accounts.txt"))
    {
       string line = null;
       while ((line = tr.ReadLine()) != null)
       {
         String[] details = line.Split('\t');
         accounts.Add(details[0].Substring(6) +
                      ":" + details[1].Substring(10));
       }
    }
}

文本文件應該包含什么/內容應該如何格式化。 我試過:用戶名:密碼用戶名/t密碼

但得到錯誤

錯誤是:

System.ArgumentOutOfRangeException: startIndex 不能大於字符串的長度。 在 System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) at System.String.Substring(Int32 startIndex, Int32 length) at System.String.Substring(Int32 startIndex) at Test.Program.loadAccounts() in c: \\Users\\Documents\\SharpDevelopProjects\\Test\\Test\\Program.cs:line 148 at Test.Program.Main(String[] args) in c:\\Users\\Documents\\SharpDevelopProjects\\Test\\Test\\Program.cs:line 26

奇怪的代碼。

這一行在文本文件中的一個制表符上拆分一行,例如“用戶名(在這里想象一個制表符)密碼”

 String[] details = line.Split('\t');

該行丟棄用戶名元素的前 6 個字符 (details[0]),添加一個冒號,丟棄密碼元素的前 10 個字符 (details[1]) 並將其連接成一個字符串。

 accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10));

文本文件中的所有行都應該有: Min user Name length = 6 Min Password length = 10 我也會用類似 ' ' 或 '-' 的東西替換 '\\t'

  • MyNameIs-MyPasswordIsTheFollowing1
  • MyNameIs-MyPasswordIsTheFollowing2
  • MyNameIs-MyPasswordIsTheFollowing3
  • MyNameIs-MyPasswordIsTheFollowing4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM