以所需格式解析上传到内存的 .txt 文件中的文本 C#
这里对 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# 页面中使用的代码
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;
}
}
}
}
}
有任何指针我应该朝哪个方向走吗?
fairly new to C# here...
...What I'm trying to do is parse a bunch of text to a webpage from a .txt file uploaded to memory.
Here's what the .txt file looks like
.RES B7=121 .RES C12=554 .RES VMAX=4.7μV
Again, it goes on like this for another 50 or so .RES'...
I've successfully got it to parse, but not in the desired format...
Here's how I want it to look on the webpage
B7.........121
C12.........554
VMAX.........4.7μV
all inside of a hidden panel with id="outpt"... which is currently set to visible="false"
Here's the code i'm using in my 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;
}
}
}
}
}
Any pointers in which direction I should go with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不确定你的格式,但简单的分割和格式就可以了。
在您的拆分中,您将获得
=
符号之前的所有内容,而根据您的描述,您需要其之后的所有内容。例如Not sure on your formatting, but a simple Split and Format will do the trick.
On your split, you're getting everything before the
=
sign, whereas from your description, you want everything after it. e.g.如果您采用 .RES B7=121 并将其拆分为“=”,则索引 0 将是 .RES B7,索引 1 将是 121
如果您想进一步细分,则必须使用 Chr(32) 再次拆分索引 0将产生 .RES 为 0,B7 为 1。
可以按照上面建议的字符串格式内联完成。
看起来你想要类似的东西
if you take .RES B7=121 and split it on "=" then the index 0 would be .RES B7 and index one would be 121
if you want to further subdivide you wil have to split index 0 again using Chr(32) which would yield .RES as 0 and B7 as 1.
Can be done inline as suggested above with string formatting.
It looks as though you want someling like
好吧..你在这里有几个问题..
首先,你使用 =.. 进行分割,所以你将字符串分成两部分......
.RES B7=121 得出:
tmpArray[0] = .RES B7
tmpArray[1] = 121
为了避免再次分割,最好的方法是将 .RES 替换为空字符串:
然后,您应该在页面上的某个位置显示它,因此您应该编写:
Ok.. you have several issues here..
first, you are spliting using the =.. so you are splitting the string in two parts...
.RES B7=121 comes to:
tmpArray[0] = .RES B7
tmpArray[1] = 121
To avoid another split, the best you can do is to replace .RES with and empty string:
Then, you should be showing this somewhere on your page, so you should be writting:
看起来您需要进行 2 次分割:
第一个
var tmpArr1 = tba.Split(' ')
将生成一个字符串数组{".RES","B7=121"}
2nd
var tmpArr2 = tmpArr1[1].split('=')
将生成一个字符串数组{"B7","121"}
然后你可以这样做:
Looks like you need to do 2 splits:
1st
var tmpArr1 = tba.Split(' ')
will result in a string array{".RES","B7=121"}
2nd
var tmpArr2 = tmpArr1[1].split('=')
will result in a string array{"B7","121"}
Then you can do: