Hello Brian,
Jesse, I appreciate all your help so far, and have now come up with
something I am once again unsure how to fix.;
In the Example.txt file I sent you, when the lines are combined into
one they are a little jumbled. I would like to format the line so
that the RX and TX portion all start at the 78th position from the
left. Everything to the left of that would never go past the 75th
position.
I know that I can perform a StringBuilder.Insert, just not sure how
or where to add it into this code.
Here is the code for what we made work.
Basically you have two options.
1) Find the location and insert as many spaces as needed.
2) Find the location, split the string. Now use
stringBuilder.AppendFormat
function
if you use the following format: "{0, -78}{1}" and the two parts of
the string
you have collected using substring it will padd the string to 78
positions
if needed:
/* might need more specific searching for RX/TX */
if (line.InfexOf("TX") > 0 || line.InfexOf("RX") > 0)
{
int charactersTillPoint = /* Find out the position */
string part0 = line.SubString(0, charactersTillPoint);
string part1 = line.SubString(charactersTillPoint);
sb.AppendFormat("{0, -78}{1}", part0, part1);
}
I've inserted where to mark it with #####
---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
sb.Append("\r\n");
#### This is where to insert
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---
Thanks,
:
Hello Brian,
I have no clue how to give text a different colour. I am sure
though that you would need to use a RichTextBox. I'd guess you'd
have to insert some RTF control characters. Where to insert them
would be easy to find out if it's always the 34th Hex pair.
private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos > 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}
It's all in one function now, but I'd place the extra checks in a
subfunction should they grow any further.
I tried to find a string function that automatically gives the xth
position of a substring (IndexOf(what, from, count, number) but I
couldn't find it... too bad
).
Again you could use a regex for this...
Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpair>[A-F0-9]{2})",
"$1" + "control character for colour" + "$2" + "control character
for normal colour");
It's actually easier for me to read than the function above, but
that is partly due to the fact that I'm presenting courses in .NET
Regular Expressions as part of my job.
Jesse
I wnt with the Stringbuilder solution. It seemed easier to follow
to me than the Regex one.
:
Hello Brian,
Did you go for the regex or the stringbuilder solution?
Jesse
Ok, Jessee, I have a new Wrinkle for you.
Lets say I want a specific hex pair highlighted in Blue on every
line that starts out with ARES_INDICATION or ARES_EINDICATION.
It would be the 34 item from the left.
If you have the Example.txt I emailed you, the last line in the
how I want it to look the field would the C7 that follows the 02
02 hex.
If not I can send it to you again.
:
Hello Brian,
Thank you Jesse, that will help a bunch. I have C# classes
scheduled Aug/Sep/Oct so that I can try to avoid more
questions in the future.
You're very welcome and don't hesitate to ask questions that is
what these newsgroups are for after all. Just make sure you've
tried
Jesse
V/R
Brian
:
Hello Brian,
You can do this quite easily with either a regex or a simple
function I'll try to demonstrate both:
basically you need a regex that matches the
\r\ndate+time+[number] that is followed by two spaces and
replace it with a space
so the regex looks like this (I simplified it a bit, but
given the fact that all the lines are very well layed out I
don't thing you have to make it more differcult
.
\r\n[^\]]+\][ ]{2}
now replace this by " " and you're all set.
So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
RegexOptions.Compiled);
private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}
Or you could use a StringReader/StringBuilder combination:
private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new