C# 需要帮助阅读一些C吗#
代码是:C# 需要帮助阅读一些C吗#,c#,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'); ac
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));
}
}
}
文本文件应该包含什么/内容应该如何格式化。
我试过:
用户名:密码
用户名/密码
但是会出现错误
错误是:
System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度。
在System.String.InternalSubStringWithChecks处(Int32 startIndex、Int32 length、布尔值)
at System.String.Substring(Int32 startIndex,Int32 length)
在System.String.Substring(Int32 startIndex)中
在中的Test.Program.loadAccounts()处
c:\Users\Documents\sharpdevelopmentprojects\Test\Test\Program.cs:第148行
在中的Test.Program.Main(字符串[]args)处
c:\Users\Documents\sharpdevelopmentprojects\Test\Test\Program.cs:第26行
奇怪的代码
此行将文本文件中的一行拆分为一个制表符字符,例如“用户名(此处为制表符)密码”
此行丢弃username元素(详细信息[0])的前6个字符,添加冒号,丢弃password元素(详细信息[1])的前10个字符,并将其连接为单个字符串
accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10));
文本文件中的所有行都应具有: 最小用户名长度=6 最小密码长度=10 我还将用类似于“”或“-”的内容替换“\t”
- MyNameIs-MyPasswordIs如下1
- MyNameIs-MyPasswordIs如下2
- MyNameIs-MyPasswordIs如下3
- MyNameIs-MyPasswordIs如下4
\t
是一个表格。i、 你的问题标题说的不多。请参考。你遇到了什么错误?那么,不是一个解决编码测验的网站,你应该知道文本文件应该包含什么以及你想要它做什么。错误如下:(甚至为什么我尝试用标签操作用户名和密码):哦,我明白了^ ^ ^现在似乎可以工作了,我也把它们都改了1,不确定它工作得有多好,但没有错误,非常感谢:D-意识到我将不得不为它做一些编辑。如果这能解决你的问题,那么请将此标记为答案:-)你也可以在切割前询问字符串长度!
accounts.Add(details[0].Substring(6) + ":" + details[1].Substring(10));