I need to know the command that I can print a sentence like "the item Peter at row 233 and column 1222 is not a number " .
I far as now I have made this:
string[] lineItems = (string[])List[]
if (!Regex.IsMatch(lineItems[0], (#"^\d*$")))
textBox2.Text += " The number ,lineItems[0], is bigger than
10 " + Environment.NewLine;
I want to print the array fields that have error. So if it finds something it will print it.
I made a code that correctly prints that there is an error on this line of the array, but I cant print the item of the array.
I need to have an Environment.NewLine because I will print many lines.
Thanks ,
George.
foreach (int lineNumber in lineItems)
{
if (lineNumber > 10)
textBox2.Text += "The number " + lineNumber + " is bigger than 10\n";
}
Something like this should work, (I have not checked the c# code, I am working on a mac at the moment)
TextBox2.Text="This is FirstLine\nThis is Second Line";
The code is not compilable absolutely, but I may be understand what you're asking about.
If you are asking about how to compose the string of text box, by adding new strings to it, based on some desicional condition (regex), you can do folowing, pseudocode:
StringBuilder sb = new StringBuidler();
if (!Regex.IsMatch(lineItems[i], (#"^\d*$")))
sb.Append(string.Format(The number ,{0}, is bigger than 10, lineItems[i]) + Environment.NewLine);
textBox2.Text = sb.ToString();
If this is not what you want, just leave the comment, cause it's not very clear from post.
Regards.
Related
when i put \t in the console.writelin() it doesn't work
if (reader.HasRows)
{
Console.WriteLine("|{0}|\t|{1}|\t|{2}|\t|{3}|\t|{4}|", reader.GetName(0),
reader.GetName(1), reader.GetName(2), reader.GetName(3), reader.GetName(4));
while (reader.Read())
{
Console.WriteLine("|{0}|\t|{1}|\t|{2} EGP|\t|{3} EGP|\t|{4}|", reader.GetString(0),
reader.GetInt32(1), reader.GetInt32(2), reader.GetInt32(3), reader.GetString(4));
}
}
the result is ::
|Product Name| |Quantity| |price per item | |Total| |Code|
|a| |1| |0 EGP| |1 EGP| |12|
even when i use {0,10} or {0,-10} it is not working
thank you.
Yes, it's work.
You sould imagine tabs as columns markers. So when you put a \t you are saying to Console: jump to the next available column mark. In your header 'Product Name' has revased the first tab column, so when Console process \t, it jumps to 2nd column. Instead, in the data, 'a' is small enought and can jump to the 1st column.
This will output with correct format:
Console.WriteLine(string.Format("|{0,-15}|", "Product Name"));
Console.WriteLine(string.Format("|{0,-15}|", "a"));
You can see it working at http://rextester.com/TUVX20333
Q
What does the \t do?
A
It moves the cursor to the next column that is a multiple of 8.
This is exactly what is happening in your example. Maybe not what you wanted but definitely what you asked for. ;)
\t really works here. But you are expecting something else. If you can mention what kind of output you are expecting. It would be easy to help.
Edit: The below code would create the header of the table as you have expected. Adjust the second number inside the {} to suit your column width.
Console.WriteLine(string.Format("|{0,-20}|{1,-20}|{2,-20}|{3,-20}|{4,-20}|", "Product Name", "Quantity", "Price per item", "Total", "Code"));
A stopgap measure, if you know the length of your columns, is to use string.PadRight. BUT it is messy:
Console.WriteLine("{0}{1}{2}{3}{4}",
("|" + reader.GetString(0) + "|").PadRight(20, ' '),
("|" + reader.GetInt32(1) + "|").PadRight(20, ' ')
("|" + reader.GetInt32(2) + "EGP|").PadRight(20, ' ')
("|" + reader.GetInt32(3) + "EGP|").PadRight(20, ' ')
("|" + reader.GetString(4) + "|").PadRight(20, ' '));
Although putting that into a separate method is straight forward.
If you really want to use \t then you can write a separate method to calculate how many tabs to add. Once again you will need to know the length of each field:
string ToTabColumn(string text, int length)
{
int tabSize = 8; // no easy way of getting environment tab length
int colSize = text.Length + Convert.ToInt32(Math.Ceil((double)(length - text.Length) / tabSize));
return text.PadRight(colSize, '\t');
}
I'm currently pooping out, I am having trouble displaying lines from a text document. What I mean is whenever I press enter it displays a new line of the text document.
Thanks
This is I guess some code, this is referenced to a text document and received a definition, from here it receives it and gets it ready to display but I just need it to read from say result one line at a time every time I press enter
First line wont work this is the first line ->>>>
var result = GetLinesWithWord(i1, #"" + Path + "/dict2.txt");
// Display the results.
foreach (var line1 in result)
{
//word maxlength
const int MaxLength = 82;
var name1 = line1;
if (name1.Length > MaxLength)
name1 = name1.Substring(0, MaxLength);
Console.WriteLine(name1 + "\r");
string boc1 = name1;
string foc1 = i1;
System.IO.File.AppendAllText (#"" + Path + "/" + n + ".txt", foc1 + "\n" + boc1 + "\n");
If I understand you correctly (especcialy your comment to #MairajAhmad) you want to dump the text file line by line, waiting for the user to press a key after each line.
Look at the post here. I think, that is what you need. It's basically a call to Console.ReadKey(true). The mentioned post listen's for the escape key, but it should still illustrate what to do.
I've been looking at other stack overflow articles regarding similar issues when it comes to word count in C#, but none have helped me when it comes to the pickle I've encountered.
I have a textbox that inputs text from a text file. The text is split into three lines by me pressing the enter button to create a new line in a text file. The text reads:
It's a test to see "if" the
application_for Top Image Systems
actually work. Hopefully it does work.
Now as you can see there should be 17 words, however my word count only says 15. I have realized after a bit of trial and error that the issue must be the fact it's in a new line. Every time it goes to a new line, it thinks the last word of the previous line and the first word of the new line are together as a word (or that's what I think the program is thinking).
My question is with the code I have below, how can I get to recognize that if there is a new line, that it should split the words like a space?
Below is my code:
string nl = System.Environment.NewLine;
//Missing Code to read text file which I don't need to include in this example
do
{
textLine = textLine + txtReader.ReadLine();
}
//Read line until there is no more characters
while (txtReader.Peek() != -1);
//seperate certain characters in order to find words
char[] seperator = (" " + nl).ToCharArray();
//number of words
int numberOfWords = textLine.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
txtReader.ReadLine(); strips your newline away.
From the msdn:
The string that is returned does not contain the terminating carriage return or line feed.
so you have to add it manually (or just add a space)
textLine = textLine + txtReader.ReadLine() + " ";
consider using the StringBuilder class for repeated concatination of strings.
Edit:
To get the character count as if the spaces were never added, do:
int charCount = textLine.Length - lineCount;
where lineCount an integer that you increment every time you add a space in your do-while loop:
int lineCount = 0;
do
{
textLine = textLine + txtReader.ReadLine();
lineCount++;
}
I'm a bit of a beginner myself, sorry if this is not a great answer, but I've just done a bunch of text stuff in c# and I'd probably approach by replacing the line breaks which will show up as "\n" or "\r" in your original string with a space, " " - something like:
nl = nl.Replace("\r", " ");
I'm reading a field On a table it only has 3 values ("",ESD,R&S)
I don't know exactly why, but when I read the R&S value, the print out label is R ("empty space") S
this is the code I'm using:
char[] area = read1[8].ToString().ToCharArray();
// if array is less than one do nothing
if (area.Length > 1)
{
//trying to use this to check if the second item of array is the "&" symbol (print this format data)
if (area[1].ToString() == "&")
{
Arealbl.Text = area[0].ToString() + "\n" + "&" + "\n" + area[2].ToString();
}
//else print out this format data
else
{
Arealbl.Text = area[0].ToString() + "\n" + area[1].ToString() + "\n" + area[2].ToString();
}
}
I using this code because I haven't found an easy way to put a label on vertical.
The & is a special char in MenuItems, Labels and Buttons, used to indicate that the next char should be underscored. When you manage to focus Arealbl and hit Alt you might see that.
Set
Arealbl.UseMnemonic = false;
somewhere. Like with the designer.
In addition to #Henk Holterman's answer, here are a few code review suggestions. You can access a string as an array, so there is no need to .ToString().ToCharArray(), just to .ToString() everything further down the method. Simplifying the concatenation to a string.Format can help improve readability and assuming you don't have to do this a large number of times (tens of thousands) it shouldn't impact performance.
string area = read1[8].ToString()
if(area.Length < 3) { return; } //exit early on error conditions.
// if array is less than one do nothing
Arealbl.UseMnemonic = false; //only add this if you cannot guarantee it will be set.
Arealbl.Text = string.Format("{0}\n{1}\n{2}", area[0], area[1], area[2]);
Hi there I have the following code-
richTextBox1.Text = richTextBox1.Text + action + "ok: " + ok.ToString();
richTextBox1.Text = richTextBox1.Text + "err: " + err.ToString();
richTextBox1.Text = richTextBox1.Text + "\r\n";
textBox1.Text = textBox1.Text;
The results look like -
ok:7err:0
But I want-
ok:7
err:0
With spacing, to make it look better how can I do this?
You could add another 2 lines:
richTextBox1.Text += Environment.NewLine;
richTextBox1.Text += Environment.NewLine;
between your "ok" and "err" - assuming you want a blank line between the two lines of output. However, you should either be using string.Format or a StringBuilder to create your output as concatenating strings this way in inefficient.
You also don't need the final:
textBox1.Text = textBox1.Text;
as that is just setting the text box contents back to itself and does nothing.
You've already got your answer, you just have it in the wrong place! The key is to use the escape sequence \r\n, which inserts a carriage return and a new line.
Also, there's no reason to split this code up into multiple lines. You end up incurring a performance penalty for doing so. It's better to do all of the string concatenation at one time. (You aren't doing enough concatenations here to justify using the StringBuilder class, but it's worth keeping in mind that strings are immutable in .NET and writing code accordingly.)
Try rewriting the code like this:
textBox1.Text = textBox1.Text + action + "ok: " + ok.ToString(); + "\r\n" +
"err: " + err.ToString(); + "\r\n";
You can also complete eliminate the last line of code, as that simply sets the value of textBox1.Text to itself. It's a no-op, meaning that it does nothing at all.
first that you could do all these in a single statement, second you could use += operator instead, and third what is that last statement doing?! it not needed, fourth add "\n" after each part you need there is no limit where you should put it, no "\r" needed.