Reputation: 193
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?
Upvotes: 1
Views: 253
Reputation: 101614
// 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();
}
Upvotes: 3
Reputation: 1564
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:
var line = String.Format("<label>{0}</label><textbox>{1}</textbox>",tmpArr2[0],tmpArr2[1]);
Response.Write(line);
Upvotes: 0
Reputation: 2138
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:
tmpArray[0] = tmpArray[0].replace(".RES","");
Then, you should be showing this somewhere on your page, so you should be writting:
Response.Write("<label>" + tmpArray[0].ToString() + "< />" + "<textbox>" + tmpArray[1].ToString() + "< />");
Upvotes: 1
Reputation: 2400
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
String.Format("<label>{0}< />.........textbox>{1}< />",tba.Split('=')[0].Split(' ')[1],tba.Split('=')[1].);
Upvotes: 1
Reputation: 32278
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.
while(...)
{
var line = String.Format("<label>{0}< />.........textbox>{0}< />",tba.Split('=')[1]);
Response.Write(line);
}
Upvotes: 2