c#字符串拆分忽略分隔符之间的空值
嘿,我已经问过了,但还没有解决,什么都没用,这是我的完整代码:c#字符串拆分忽略分隔符之间的空值,c#,string,debugging,split,outofrangeexception,C#,String,Debugging,Split,Outofrangeexception,嘿,我已经问过了,但还没有解决,什么都没用,这是我的完整代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Insertss { class Program { static void Main() { ReadWrite("projektangebote.txt", "in
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Insertss
{
class Program
{
static void Main()
{
ReadWrite("projektangebote.txt", "inserts.txt");
}
static void ReadWrite(string readName, string writeName)
{
StreamReader streamReader;
StreamWriter streamWriter;
string str;
List<string> values = new List<string>();
streamReader = File.OpenText(readName);
str = streamReader.ReadLine();
streamWriter = new StreamWriter(writeName);
while (str != null)
{
values.Add(str);
str = streamReader.ReadLine();
}
foreach (string a in values)
{
int temp = 1;
String[] temparray = a.Split(';');
streamWriter.WriteLine("Insert into table Firma values({0},'{1}','{2}')", temp, temparray[1], temparray[4]);
temp++;
}
streamReader.Close();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
名称空间插入
{
班级计划
{
静态void Main()
{
ReadWrite(“projektangebote.txt”、“inserts.txt”);
}
静态void ReadWrite(字符串readName、字符串writeName)
{
StreamReader StreamReader;
StreamWriter StreamWriter;
字符串str;
列表值=新列表();
streamReader=File.OpenText(readName);
str=streamReader.ReadLine();
streamWriter=新的streamWriter(writeName);
while(str!=null)
{
添加(str);
str=streamReader.ReadLine();
}
foreach(值中的字符串a)
{
内部温度=1;
字符串[]temparray=a.Split(“;”);
WriteLine(“插入表Firma值({0}、{1}、{2}”)、temp、temparray[1]、temparray[4]);
temp++;
}
streamReader.Close();
}
}
}
好的,我插入:
帕鲁阿贝斯蒂尔;普里乌·贝雷希;公司名称;公关机构;普鲁马斯纳姆;公共政策支持;项目团队日期项目团队日期
Überarbeitung der SAV Seite;b、 i.b。;;;;铅
但是第二排(b.i.b.;;PB;)我得到了一个索引自动异常,因为: 有人知道我如何解决这个问题吗? 要么只插入一些字符,要么插入一个空格 JAVA应用程序;b、 i.b;诺瓦比格 这是您正在拆分的字符串->请参阅调试器变量
a
,您还可以看到temparray包含我显示的字符串的部分
您可能读取了错误的文件(检查名称和目录)。您可以手动检查在调试器中读取的行,并查看它是否与预期值匹配
拆分字符串在这里工作得很好,对于字符串
a
它应该给出3的数组,因为字符串a
只有两个
的您的字符串是JAVA应用程序;b、 i.b;Novabig….
因此它有3个条目请参见debuger@DGibbs是的,我问了,但没有任何效果,我应该在那里张贴我的全部代码guess@wudzik是的,这就是问题所在read@user3107760他说您在调试器中显示不同的字符串(这显然是抛出异常),你在问题中没有提到任何一个;b、 i.b。;;;;PB我想要分裂:萨瓦塞特河流域;b、 i.b。;;;;PB我希望它返回一个7的数组elements@user3107760你想分割什么并不重要。重要的是你到底是什么splitting@user3107760正如我所建议的,尝试检查您正在打开的文件,以及该文件是否包含您期望的数据。没有那个文件的内容,我不能再提了。现在我看你错了string@wudzik哦,现在我明白你的意思了,我的文件看起来像这样: