Get a specific word from a line read from a .txt - c#

right now I am reading some lines from a .txt.
Lets say, a user enters his name and in the .txt will be saved "Logged in {username} on 13/04/2016 at 10:55 am".
(Just an example.)
Now I want to read the .txt and print only specific parts into a textbox.
Meaning, in the textbox shall appear "{Username} - 13/04/2016 - 10:55 am".
So far, I am able to read from the .txt and print the whole line.
private void button_print_results_Click(object sender, RoutedEventArgs e)
{
int counter = 0;
string actual_line;
System.IO.StreamReader file_to_read =
new System.IO.StreamReader("myText.txt");
while ((actual_line = file_to_read.ReadLine()) != null)
{
textBox_results.Text = textBox_results.Text +"\n"+ actual_line;
counter++;
}
file_to_read.Close();
}
Is there a way, to reach this without overwriting the whole file?
And no, I can't change the format how the names etc. are saved.
(I used them here for a better understanding, the actual lines I need to read/check are different and auto-generated).
I don't expect full working code, it would be just great if you could tell me for which commands I need to look. Been a long time since I last worked with c#/wpf and I never worked much with Streamreader...
Thanks

I think regular expressions is the best tool for what you're trying to achieve. You can write something like this:
Regex regex = new Regex("Logged in (?<userName>.+) on (?<loginTime>.+)");
while ((actual_line = file_to_read.ReadLine()) != null)
{
Match match = regex.Match(actual_line);
if (match.Success) {
string loginInfo = string.Format("{0} - {1}", match.Groups["userName"], match.Groups["loginTime"]);
textBox_results.Text = textBox_results.Text +"\n"+ loginInfo;
}
}

There are couple of possible solutions for this. One most straight forward way for your case would be to use Substring and Replace.
Since the earlier string is always Logged in (note the last space) and you simply want to get the rests of the string after the phrase, replacing only the preposition of time words (" on ", " at ") with dash (" - ") you could take advantage on that:
string str = "Logged in {username} on 13/04/2016 at 10:55 am";
string substr = str.Substring(("Logged in ").Length) //note the last space
.Replace(" on ", " - ")
.Replace(" at ", " - ");
In your implementation, this is how it look like:
while ((actual_line = file_to_read.ReadLine()) != null)
{
actual_line = actual_line.Substring(("Logged in ").Length) //note the last space
.Replace(" on ", " - ")
.Replace(" at ", " - ");
textBox_results.Text = textBox_results.Text +"\n"+ actual_line;
counter++;
}
(Note: the solution above assumes the {username} does not contain spaced preposition of time words - which would almost likely be the case for a {username})

You could split the actual_line String so you get an array of Strings. And then fill the Strings you want to show in the TextBox into it.
string[] values = actual_line.Split(' ');
textBox_results.Text = textBox_results.Text + "\n" + values[2] + " " + values[6] + " " + values[7];
The text in the TextBox for example is "{username} 10:55 am"

You can use Regex for better performances as #Dmitry-Rotay suggested in the previous comment, but if you jave a not-so-big file your loop+string manipulations is an acceptable compromise.
Always use Environment.NewLine instead of "\n", it's more portable.
while ((actual_line = file_to_read.ReadLine()) != null)
{
actual_line = actual_line
.Replace(("Logged in "), String.Empty)
.Replace(" on ", " - ")
.Replace(" at ", " - ");
textBox_results.Text = textBox_results.Text
+ System.Environment.NewLine
+ actual_line;
counter++;
}

Related

StreamReader from .csv - "foreign" chars and blank values showing up as '?'

I'm having two problems with reading my .csv file with streamreader. What I'm trying to do is get the values, put them into variables which I'll be using later on, inputting the values into a browser via Selenium.
Here's my code (the Console.Writeline at the end is just for debugging):
string[] read;
char[] seperators = { ';' };
StreamReader sr = new StreamReader(#"C:\filename.csv", Encoding.Default, true);
string data = sr.ReadLine();
while((data = sr.ReadLine()) != null)
{
read = data.Split(seperators);
string cpr = read[0];
string ydelsesKode = read[1];
string startDato = read[3];
string stopDato = read[4];
string leverandoer = read[5];
string leverandoerAdd = read[6];
Console.WriteLine(cpr + " " + ydelsesKode + " " + startDato + " " + stopDato + " " + leverandoer + " " + leverandoerAdd);
}
The code in and of itself works just fine - but I have two problems:
The file has values in Danish, which means I get åøæ, but they're showing up as '?' in console. In notepad those characters look fine.
Blank values also show up as '?'. Is there any way I can turn them into a blank space so Selenium won't get "confused"?
Sample output:
1372 1.1 01-10-2013 01-10-2013 Bakkev?nget - dagcenter ?
Bakkev?nget should be Bakkevænget and the final '?' should be blank (or rather, a bank space).
"Fixed" it by going with tab delimited unicode .txt file instead of .csv. For some reason my version of excel doesn't have the option to save in unicode .csv...
Don't quite understand the problem of "rolling my own" parser, but maybe someday someone will take the time to explain it to me better. Still new-ish at this c# stuff...

String randomly containing a newline C#

I'm completely lost with what is happening here.
string send = "!points add " + entries[winner] + " " + prize.ToString();
What I want to send is "!points add winnername prizeamount" but what I get is "!points add winnername\nprizeamount". I put \n because it writes a new line but trying to replace "\n", "\r" and "\t" with " " does nothing.
enter image description here
all I need is the message to be exactly"!points space add space winnername space prizeamount space"
If it's important the entries in my code is a List of strings
The entries strings already contain the new line character(s).
I suggest you replace with Environment.NewLine:
Replace Line Breaks in a String C#
The string object, 'entries[winner]' is having line-feeds (LF) or carriage-returns (CR). You try this to remove all LFs and CRs,
string send = "!points add "
+ entries[winner].Replace("\r", string.Empty).Replace("\n", string.Empty)
+ " " + prize.ToString();
Alternatively, you can use Trim() to remove leading\ trailing LFs\ CRs.
string send = "!points add "
+ entries[winner].Trim()
+ " " + prize.ToString();
No repro. The following code doesn't assert.
var entries=new List<string>{"Aaa", "Bbb", "Ccc"};
int prize=90;
int winner=1;
var send=String.Format("!points add {0} {1}",entries[winner],prize);
var send2="!points add " + entries[winner] + " " + prize.ToString();
Trace.Assert("!points add Aaa 90"==send);
Trace.Assert(send2==send);
If the result contains newlines, it's because the entries values contain newlines.
The best solution would be to clean the input data before storing it in the list, eg with String.TrimEnd or String.Trim, When loading data from a file for example, you can't be sure it doesn't contain trailing spaces.
To read clean data from a file you could use :
var entries=File.ReadLines()
.Select(line=>line.Trim())
.ToList();
If you add the entries one by one from user input :
entries.Add(newEntry.Trim());
If you can't change how the data is read (why?) you can trim when whenever you use an entry value:
var send=String.Format("!points add {0} {1}",entries[winner].Trim(),prize);
Loading clean data is a lot easier

How to slice/trim this value?

I am trying to remove the last 6 characters from item.Size because the data has a decimal place and 5 trailing 0s in the database.
sb.Append("<div>" + item.Size + " " + item.Units + " </div>");
ie. item.Size is displayed as 1.00000 and I need it to just be displayed as 1.
This is part of a StringBuilder, and as I'm new to coding, not even sure the right way to go about this.
sb.Append("<div>" + (int)item.Size + " " + item.Units + " </div>");
StringBuilder has the same formatting capabilities as String.Format when you use the AppendFormat method:
sb.AppendFormat("<div>{0:N0} {1} </div>", item.Size, item.Units);
The format string "N0" tells it to format the number with 0 decimal points. That assumes the item.Size is stored as a numerical type. If not, simply remove the part of the string you don't want:
sb.AppendFormat("<div>{0} {1}</div>", item.Size.Split('.')[0], item.Units);
Here I've used Split, assuming that the value is actually something like what you've shown in your example.
Better you use int.TryParse(or Int32.TryParse) method. because if item.Size is not convertible to int, then it wont give you any exception. you can use int or long according to your choice. So you can handle this in your code according to the if/else condition.
Sample Code:
int size;
string str = "";
if(int.TryParse(str, out size) == true)
{
}
else
{
}

set string to a fixed space?

I have some vehicle information that I want to send in an email.
I have all code working but spacing out the information is a problem. Each vehicle has a checklist and that checklist then gets emailed. So I loop through the list and get the defect and the comment.
foreach (var item in chkList.CheckItems)
{
if (item.Defect == true)
{
defect += string.Format("{0,-40} {1}\n", item.ItemTitle, item.Comment);
}
}
if (hasDefect == true)
{
Utils.ChecklistSendMail("Checklist", ToAddresses.Split(';'),
"Vehicle Reg: " + reg + "\n" +
"Checklist No: " + chkList.CheckListNo + "\n"+
"Date: " + ChecklistDate.ToShortDateString() + "\n" +
"Defects: Comments: " + "\n" +
defect);
}
Email then looks like this:
Vehicle Reg: XLZ 8194
Checklist No: 0
Date: 22/03/2016
Defects: Comments:
Vehicle Secure comment1
Brakes comment2
I want the defects and the comments to be displayed like this:
Defects: Comments:
Vehicle Secure comment1
Brakes comment2
Since Vehicle Secure is longer than Brakes the comment is being pushed further out. But is there a way to fix the string at a certain position no matter how long first word is?
EDIT
code now looks like this:
string defect = "";
string comment = "";
string aheading = "Defects:";
string bheading = "Comments:";
foreach (var item in chkList.CheckItems)
{
if (item.Defect == true)
{
defect += item.ItemTitle;
comment += item.Comment;
}
}
string result = aheading.PadRight(20, ' ') + bheading.PadRight(20, ' ') + Environment.NewLine +
defect.PadRight(20, ' ') + comment.PadRight(20, ' ') + Environment.NewLine;
But the output looks like this:
Defects: Comments:
Vehicle SecureBrakestest1test2
If you really want to do this with spaces, you need to determine the label with the most characters and how much space you want after that. This creates a total number of characters. From this, you subtract the number of characters of the label to get the number of spaces necessary to line up the value.
However, you could just use a <table> or some other html.
A potential, quick and dirty solution would require you to generate the html as a part of your code. I seriously advise against some homegrown html generator logic. Invariably the data involved in the email becomes more complex. This leads to mixing code that is getting the data for the template and building the html, which is painful to debug. Also there are plenty of html templating solutions out there. You'd really be just reinventing the wheel to take on technical debt and the maintenance of more code.
A better solution would be use something like MvcMailer and build an html template. You then pass the template and a context object to the engine to render the resultant html.
Try and use String padding with ' ' as char
public string PadRight(
int totalWidth,
char paddingChar)
This method would complete the length of the string with the chosen char. by specifying the max length of the string and replacing the remaining length with " " (space). you can always have the strings aligned.
Read more about PadRight or PadLeft
string Defects ="Example"
Defects.PadRight(20," ");
Result: "Example "
Edit : Example Code .Please have a look at this code and check what you are doing wrong
string aheading = "Defects:";
string bheading ="Comments:";
string b = "Vehicle Secure";
string bComment = "My Comment value";
string c = "Brakes";
string cComment = "My Comment value";
string result= aheading.Trim().PadRight(20,' ')+bheading.Trim().PadRight(20,' ')+ Environment.NewLine +
b.Trim().PadRight(20, ' ') + bComment.Trim().PadRight(20, ' ') + Environment.NewLine +
c.Trim().PadRight(20,' ')+cComment.Trim().PadRight(20,' ')+Environment.NewLine ;
Console.WriteLine(result);
Edit:Answer based on the code you Posted
string aheading = "Defects:";
string bheading = "Comments:";
string result = aheading.PadRight(20, ' ') + bheading.PadRight(20, ' ') + Environment.NewLine ;
foreach (var item in chkList.CheckItems)
{
if (item.Defect == true)
{
string result += item.ItemTitle.Trim().PadRight(20,' ') + item.ItemTitle.Trim().PadRight(20,' ') + Environment.NewLine ;
}
}
Console.WriteLine(result);

Crystal report textobject and fields

i am running the following code
foreach (ReportObject obj in oSectionObjects)
{
if (obj.Kind == CrystalDecisions.Shared.ReportObjectKind.TextObject)
{
// do stuff
}
}
but i have a problem. i do have multiple text that do contain text AND fields in them.
But crystal return me the field being TextObject which is technically true.
How do i know i ONLY have text in the TextObject and not anything else (aka fields, parameters, formulas) ?
As far as I know the fields in a text box will be recognized by the text pattern. Try to search the text of the text object for {1#xxxxx} where xxxxx is the field name. "{1#" shows the type of the field: 1 is for a database , 2 is for formula, 3 is for parameter. You may try also for {#xxxxx} *(without numeric field identifier)
I searched alot around and found working solution for RAS report but nothing for crystal. Anyhow if someone end up here looking for an answer here's the work around.
Whenever you have to concatenate multiple fields on the report do NOT use TextObject. Instead use a Formula. The formula fields wont bet part of the ReportObjects but instead part of the ReportDocument.DataDefinition.FormulaFields with Kind being CrystalDecisions.Shared.FieldKind.FormulaField and you will want to check the ValueType so it is CrystalDecisions.Shared.FieldValueType.StringField.
then you can manipulate them.
I did need that for translation of report live so here's a parsing method for formulas :
try
{
var sFormula = formula.Text;
string pattern = "\"[\\w ]*\"";
Regex r = new Regex(pattern);
MatchCollection mc = r.Matches(sFormula);
foreach (Match m in mc)
{
var sValue =m.Value;
var sParsedValue = sValue.Substring(1, sValue.Length - 2);
if (sParsedValue.StartsWith("s"))
{
var stest = "\"" + CApplicationData.TranslateStringValue(sParsedValue) + "\"";
sFormula = sFormula.Replace(sValue, stest);
}
}
formula.Text = sFormula;
}
catch{}
this above you will notice i use 's' as a key to know it might be a value to be translated so it's not mandatory. using the above on this formula with Spanish language :
"sPage" + " " + totext(PageNumber) + " " + "sOf" + " " + totext(TotalPageCount)
will modify the formula to :
"Página" + " " + totext(PageNumber) + " " + "de" + " " + totext(TotalPageCount)
giving output of :
Página 1 de 4

Categories

Resources