【发布时间】:2013-04-09 09:32:53
【问题描述】:
我正面临一个非常有趣的问题。 以下是我要从中获取子字符串的字符串。
1¨Ñ§ËÇÑ´1xxxxxxxx
索引0-2(长度3)是省份ID 索引 3-35(长度 32)是泰语的省名。
当我尝试像下面这样取子字符串时
string line = "1¨Ñ§ËÇÑ´1xxxxxxxx ";
line.Substring(3,32).Trim();
这显示我的错误如下
Index and length must refer to a location within the string.
请记住,当我调试时,代码行总长度显示为 33,应该是 35。省子字符串显示为 30。
以下是代码...
using (StreamReader streamReader = new StreamReader("File06.txt"))
{
Console.WriteLine(".................. Content of " + file.Name + "..............");
string codeOfProvince, nameOfProvince, line;
while ((line = streamReader.ReadLine()) != null)
{
codeOfProvince = line.Substring(0, 3).Trim();
nameOfProvince = line.Substring(3,32).Trim();
Console.WriteLine("codeOfProvince {0}, nameOfProvince {1}", codeOfProvince , nameOfProvince );
}
Console.WriteLine("..............................................................");
Console.WriteLine();
我相信这会对您有所帮助,并且该文件包含以下数据
1¨Ñ§ËÇÑ´1xxxxxxxx
2¨Ñ§ËÇÑ´2xxxxxxxx
【问题讨论】:
-
整个字符串的长度是 33。你正在做
Substring(3, 32)它将超过 33 个字符,因此错误。 -
你不能使用
Substring(3)来完成所有事情吗? -
但是当我在我给它的字符串中计算 i 时,它有 35 个长度。看看“1¨Ñ§ËÇÑ´1xxxxxxxx”中有多少个字符。我在哪里声明了字符串。
-
33 是调试时显示的字符数。但实际上有 35 个字符。为什么 VS 缺少 2 个字符。尝试两次在 VS 中运行这段代码。
-
你从哪里得到字符串?它在哪里 包含 35 个字符?您如何将其添加到您的应用程序中?