对于C#来说相当新...这里...我想要做的是从上传到内存的.txt文件中解析一堆文本到网页
这是.txt文件的样子
.RES B7=121 .RES C12=554 .RES VMAX=4.7μV
再一次,它继续像这样继续50个.RES'......
我已经成功地解析了它,但没有达到所需的格式...
以下是我希望它在网页上查看的内容
B7.........121
C12.........554
VMAX.........4.7μV
所有隐藏面板内的id =“outpt”...当前设置为visible =“false”
这是我在C#Page
中使用的代码namespace Sdefault
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpld_Click(object sender, EventArgs e)
{
Stream theStream = file1.PostedFile.InputStream; //uploads .txt file to memory for parse
using (StreamReader sr = new StreamReader(theStream))
{
string tba;
while ((tba = sr.ReadLine()) != null)
{
string[] tmpArray = tba.Split(Convert.ToChar("=")); //Splits ".RES B7=121" to "B7"... but for some reason, It prints it as
//".RES B7.RES C12.RES VMAX"... I would ultimately desire it to be printed as shown above
Response.Write(tmpArray[0].ToString());
var.Text = tba;
outpt.Visible = true;
}
}
}
}
}
我应该选择哪个指示?
答案 0 :(得分:3)
// Open the file for reading
using (StreamReader reader = new StreamReader((Stream)file1.PostedFile.InputStream))
{
// as long as we have content to read, continue reading
while (reader.Peek() > 0)
{
// split the line by the equal sign. so, e.g. if:
// ReadLine() = .RES B7=121
// then
// parts[0] = ".RES b7";
// parts[1] = "121";
String[] parts = reader.ReadLine().split(new[]{ '=' });
// Make the values a bit more bullet-proof (in cases where the line may
// have not split correctly, or we won't have any content, we default
// to a String.Empty)
// We also Substring the left hand value to "skip past" the ".RES" portion.
// Normally I would hard-code the 5 value, but I used ".RES ".Length to
// outline what we're actually cutting out.
String leftHand = parts.Length > 0 ? parts[0].Substring(".RES ".Length) : String.Empty;
String rightHand = parts.Length > 1 ? parts[1] : String.Empty;
// output will now contain the line you're looking for. Use it as you wish
String output = String.Format("<label>{0}</label><input type=\"text\" value=\"{1}\" />", leftHand, rightHand);
}
// Don't forget to close the stream when you're done.
reader.Close();
}
答案 1 :(得分:2)
不确定您的格式,但是简单的拆分和格式就可以解决问题。
在你的分割中,你得到=
符号之前的所有内容,而根据你的描述,你想要它之后的所有内容。 e.g。
while(...)
{
var line = String.Format("<label>{0}< />.........textbox>{0}< />",tba.Split('=')[1]);
Response.Write(line);
}
答案 2 :(得分:1)
如果你把.RES B7 = 121并将它拆分为“=”那么索引0将是.RES B7而索引1将是121
如果你想进一步细分,你必须使用Chr(32)再次拆分索引0,这将产生.RES为0,B7为1。
可以按照上面的建议使用字符串格式进行内联。
看起来好像你想要像
一样String.Format("<label>{0}< />.........textbox>{1}< />",tba.Split('=')[0].Split(' ')[1],tba.Split('=')[1].);
答案 3 :(得分:1)
好的..你这里有几个问题..
首先,你使用= ..进行拆分,所以你将字符串拆分为两部分...... .RES B7 = 121来: tmpArray [0] = .RES B7 tmpArray [1] = 121
为避免另一次拆分,您可以做的最好的事情是将.RES替换为空字符串:
tmpArray[0] = tmpArray[0].replace(".RES","");
然后,你应该在你的页面上的某个地方展示这个,所以你应该写:
Response.Write("<label>" + tmpArray[0].ToString() + "< />" + "<textbox>" + tmpArray[1].ToString() + "< />");
答案 4 :(得分:0)
看起来你需要进行2次拆分:
第一个var tmpArr1 = tba.Split(' ')
将产生一个字符串数组{".RES","B7=121"}
第二个var tmpArr2 = tmpArr1[1].split('=')
将生成一个字符串数组{"B7","121"}
然后你可以这样做:
var line = String.Format("<label>{0}</label><textbox>{1}</textbox>",tmpArr2[0],tmpArr2[1]);
Response.Write(line);