My Goal: I want to parse a file and display it in a textbox. Here's the code (thanks to Aviral Singh).
private void Form1_Load(object sender, EventArgs e)
{
var path = #"C:\Users\Smith\Desktop\Settings.txt"; //Path to settings file.
RichTextBox rtb = new RichTextBox();
System.IO.StreamReader sis = new System.IO.StreamReader(path);
rtb.Text = sis.ReadToEnd();
sis.Close();
foreach (string line in rtb.Lines)
{
if (line.Contains("Installation Technical Manual:") == true)
{
string numbers = line.Substring(line.IndexOf("Installation Technical Manual:"));
textBox1.Text = numbers;
}
}
}
Text file looks like this:
My Problem: The textbox in my program displays entire line: Installation Technical Manual: (1234567890).
I just want the number with brackets (1234567890). to be displayed in the textbox. What changes should I make to the code to remove the words and just display numbers with brackets around it? Thanks for your help. :)
numbers = "(" + new String(numbers.Where(Char.IsDigit).ToArray()) + ")";
The problem is that IndexOf() is going to give you the starting position of the string which you're asking for the index of. Which in your example is going to be 0, so a Substring call that starts at 0 is going to return you the entire string.
What you really want to do is Substring(IndexOf("Installation Technical Manual:") + "Installation Technical Manual:".Length). That will give you whatever comes after your string.
Assuming you have ':' in your line always,
if(line.Contains(":"))
{
string numbers = line.Split(':')[1];
textBox1.Text = numbers;
}
The reason your code is not working as you have used the substring method as with one parameter, so it is used as :
public string Substring(
int startIndex
)
See: http://msdn.microsoft.com/en-us/library/hxthx5h6.aspx
And you are passing in, line.IndexOf("Installation Technical Manual:")
which will return ).
See here: http://msdn.microsoft.com/en-us/library/k8b1470s.aspx
So you are essentially saying return Substring of string Installation Technical Manual:(number) starting at 0 which basically returns the whole line.
Related
Alright, so I'm writing an application that needs to be able to extract a VAT-Number from an invoice (https://en.wikipedia.org/wiki/VAT_identification_number)
The biggest challenge to overcome here is that as apparent from the wikipedia article I have linked to, each country uses its own format for these VAT-numbers (The Netherlands uses a 14 character number while Germany uses a 11 character number).
In order to extract these numbers, I throw every line from the invoice into an array of strings, and for each string I test if it has a length that is equal to one of the VAT formats, and if that checks out, I check if said string also contains a country code ("NL", "DE", etc).
string[] ProcessedFile = Reader.ProcessFile(Input);
foreach(string S in ProcessedFile)
{
RtBEditor.AppendText(S + "\n");
}
foreach(string X in ProcessedFile)
{
string S = X.Replace(" ", string.Empty);
if (S.Length == 7)
{
if (S.Contains("GBGD"))
{
MessageBox.Show("Land = Groot Britanie (Regering)");
}
}
/*
repeat for all other lenghts and country codes.
*/
The problem with this code is that 1st:
if there is a string that happens to have the same length as one of the VAT-formats, and it has a country code embedded in it, the code will incorrectly think that it has found the VAT-number.
2nd:
In some cases, the VAT-number will be included like "VAT-number: [VAT-number]". In this case, the text that precedes the actual number will be added to its length, making the program unable to detect the actual VAT-Number.
The best way to fix this is in my assumption to somehow isolate the VAT-Number from the strings all together, but I have yet to find a way how to actually do this.
Does anyone by any chance know any potential solution?
Many thanks in advance!
EDIT:
Added a dummy invoice to clarify what kind of data is contained within the invoices.
As someone in the comments had pointed out, the best way to fix this is by using Regex. After trying around a bit I came to the following solution:
public Regex FilterNormaal = new Regex(#"[A-Z]{2}(\d)+B?\d*");
private void BtnUitlezen_Click(object sender, EventArgs e)
{
RtBEditor.Clear();
/*
Temp dummy vatcodes for initial testing.
*/
Form1.Dummy1.VAT = "NL855291886B01";
Form1.Dummy2.VAT = "DE483270846";
Form1.Dummy3.VAT = "SE482167803501";
OCR Reader = new OCR();
/*
Grab and process image
*/
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
Input = new Bitmap(openFileDialog1.FileName);
}
catch
{
MessageBox.Show("Please open an image file.");
}
}
string[] ProcessedFile = Reader.ProcessFile(Input);
foreach(string S in ProcessedFile)
{
string X = S.Replace(" ", string.Empty);
RtBEditor.AppendText(X + "\n");
}
foreach (Match M in FilterNormaal.Matches(RtBEditor.Text))
{
MessageBox.Show(M.Value);
}
}
At first, I attempted to iterate through my array of strings to find a match, but for reasons unknown, this did not yield any results. When applying the regex to the entire textbox, it did output the results I needed.
Hello sorry if the answer for this is already out there somewhere but I did not find it, or did not understand it. What I have to do is I have a list box in visual studio (C#) and in this list box are some options to click on.
PinturaV $100
PinturaA $105
PinturaE $115
Lijas_Ag $112
Solvente $101
If I select a line for example PinturaA $105 from the list box in a text box that I have it should show only 105. Then if I select for example Lijas_Ag $112, the text box should show...
105
112
And so on until I press the total button and it would give me the total.
My problem is having just the numbers appear on the text box. I coded so that the program can read the line selected and tell me where the space is located but I don't know if this was even needed to be able to get the number into the text box. I have no Idea how to do this. Any help would be much appreciated. I left some pics in case that helps.
Thank You
main selected
Code I have so far
You should use Regex to get your desired output. You need to apply Regex in your ListBox event. Please check below for Regex & C# example.
Regex:
\d+
Above Regex will give you all digit from your string
CODE C#:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Your others code & logic
//string data = "PinturaV $100";
string data = listBox1.Text;
textBox1.Text = Regex.Match(data, #"\d+").Value;
}
Considering the selected text is PinturaA $105 .. you can use regular expression (or) string library method to get only the numbers like
string str = "PinturaA $105";
string val = (str.IndexOf("$") > 0)
? str.Substring(str.IndexOf("$"), str.Length - str.IndexOf("$"))
: string.Empty;
You can do this easily with a regex:
var oldtext = "Solvente $101";
var newtest = Regex.Match(oldText, "\\d+$").Groups[0].Value;
The variable newtext will contain the digits.
You could use Regex and just do
var item = "Solvente $101";
var val = Regex.Match(item, #"\$(\d+)").Groups[1];
The other answers are OK and will probably work. But just so you know, the typical way to handle this sort of thing is to put the data you want in the ListItems' Value property and the text in the Text property, like this:
private void PopulateListBox1()
{
listBox1.Add(new ListItem("100","PinturaV $100"));
listBox1.Add(new ListItem("105","PinturaA $105"));
listBox1.Add(new ListItem("115","PinturaE $115"));
listBox1.Add(new ListItem("112","Lijas_Ag $112"));
listBox1.Add(new ListItem("101","Solvente $101"));
}
And then get the Value instead of Text, like this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedValue;
}
As you can see this is far, far simpler than parsing the string returned by ListItem.Text.
I have a list of strings stored in an ArrayList. I want to split them by every occurrence of ';'. The problem is, whenever I try to display them using MessageBox, there's an excess space or unnecessary value that gets displayed.
Sample input (variable = a):
Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;
Below is a line of code I used to split them:
string[] display_document = (a[0] + "").Split(';');
Code to display:
foreach (object doc_properties in display_document)
{
TextBox aa = new TextBox();
aa.Font = new Font(aa.Font.FontFamily, 9);
aa.Text = doc_properties.ToString();
aa.Location = new Point(pointX, pointY);
aa.Size = new System.Drawing.Size(80, 25);
aa.ReadOnly = true;
doc_panel.Controls.Add(aa);
doc_panel.Show();
pointY += 30;
}
The output that displays are the following:
How do I remove the last occurrence of that semicolon? I really need help fixing this. Thank you so much for all of your help.
Wouldnt It be easiest to check if the input ends with a ";" before splitting it, and if so remove the last character? Sample code:
string a = "Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
if (a.EndsWith(";"))
{
a = a.Remove(a.LastIndexOf(";"));
}
//Proceed with split
Split will not print last semicolon if no space character is added and your input is a string.
I don't know why you prefer an array list (which probably is the reason of this strange behaviour) but if you could use your input as a string you could try that
string a = "Arial;16pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
string[] display_document = a.Split(';');
foreach (object doc_properties in display_document)
{
//The rest of your code
}
I Have a textfile that has a value of multiple row text that don't have delimiter.
Here is the sample text on my textfile.
000100000080020201000000005309970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003F
00010001008002020100010000530997000014820000148200010000012C00001482000014820000148200010000012C000014820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000000000000000000000000000000000000003F
then i must devide each every line in this format.
"XXXXXXXX-XXXX-XXXXXXXXXXXXXX-XX-XXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXX-XX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX".Split('-');
the out put is like this.
00010000-0080-02020100000000-53-0997-00000000-00000000-0000-00000000-00000000-00000000-00000000-0000-00000000-00000000-00000000-00000000-0000-00000000-00000000-00000001-00000000-0000-00000000-00000000-00000000-00000000-0000-00000000-00000000-000000-00-00000000-00000000-0000-00000000-0000003F
00010001-0080-02020100010000-53-0997-00001482-00001482-0001-0000012C-00001482-00001482-00001482-0001-0000012C-00001482-00000000-00000000-0000-00000000-00000000-00000000-00000000-0000-00000000-00000000-00000000-00000000-0000-00000000-00000000-010100-00-00000000-00000000-0000-00000000-0000003F
i imported it into a multiline textbox.
here is my code
private void btn_input_Click(object sender, EventArgs e)
{
string content;
content = File.ReadAllText(txt_path.Text);
string[] patern = "XXXXXXXX-XXXX-XXXXXXXXXXXXXX-XX-XXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXX-XX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XXXXXXXX".Split('-');
string mystring = content;
string regex = string.Empty;
string match = string.Empty;
for (int i = 0; i < patern.Length; i++)
{
regex += #"(\w{" + patern[i].Length + "})";
match += "$" + (i + 1).ToString() + "-";
}
match = match.Substring(0, match.Length - 1);
txt_textfile.Text = Regex.Replace(mystring, regex, match);
}
then i want to save to my database the text that i split in ('-'). but i dont know how to do it. I want to ask is there is something i can do for me to able to save it. Even in while importing it. or after importing it. anyway. Please Help. Thank you
Your Question is not that much clear.
all you have done there is format your input string.
I'm not sure about the way that you want to insert the values to database.
just guessing that you want to split the formatted text by '-' and insert that split values
one by one
in that case what you have to do is split the text in txt_textfile text box and get string array.
loop the array and insert values the database.
if this is not the answer that you looked for please comment here the exact thing that you want to do
Thanks :)
Since I have not been able to find an resolution via my searching endeavor, I believe I may have a unique problem. Essentially I am creating a gene finding/creation application in c#.NET for my wife and am using RichTextBoxes for her to be able to highlight, color, export, etc the information she needs. I have made several custom methods for it because, as I am sure we all know, RichTextBoxes from Microsoft leave much to the imagination.
Anyway, here is my issue: I need to be able to search for a term across hard returns. The users have strings in 60 letter intervals and they need to search for items that may cross that hard return barrier. For instance let's say I have 2 lines (I will make them short for simplicity):
AAATTTCCCGGG
TTTCCCGGGAAA
If the user runs a search for GGGTTT, I need to be able to pull the result even though there is a line break/hard return in there. For the life of me I cannot think of a good way to do this and still select the result in the RichTextBox. I can always find the result but getting a proper index for the RichTextBox is what eludes me.
If needed I am not against richTextBox.SaveFile() and LoadFile() and parsing the rtf text as a string manually. It doesnt have to be pretty, in this case, it just has to work.
I appreciate any help/guidance you may give.
Here is a relevant snippet:
//textbox 2 search area (examination area)
private void button5_Click(object sender, EventArgs e)
{
textBox3.Text = textBox3.Text.ToUpper();
if (textBox3.Text.Length > 0)
{
List<string> lines = richTextBox2.Lines.ToList();
string allText = "";
foreach (string line in lines)
allText = allText + line.Replace("\r", "").Replace("\n", "");
if (findMultiLineRTB2(allText, textBox3.Text) != -1)
{
richTextBox2.Select(lastMatchForRTB2, textBox3.Text.Length);
richTextBox2.SelectionColor = System.Drawing.Color.White;
richTextBox2.SelectionBackColor = System.Drawing.Color.Blue;
}//end if
else
MessageBox.Show("Reached the end of the sequence", "Finished Searching");
}//end if
}//end method
private int findMultiLineRTB2(string rtbText, string searchString)
{
lastMatchForRTB2 = rtbText.IndexOf(searchString, lastMatchForRTB2 + 1);
return lastMatchForRTB2;
}
So i make an assumption: you want to search a word across all lines where each line is 60 characters long. The desired result is the index of that word.
You just have to build a string that has no line breaks, for example with string.Join:
string allText = string.Join("", richTextBox.Lines);
int indexOf = allText.IndexOf("GGGTTT"); // 9 in your sample