Hey,

I'm in the process of writing an email header analyzer in C# and i'm stuck on something. Before i go into the problem i think i should write an intro on what i'm looking to achieve. I basically want a program that can read through headers and copy some of the contents i.e senders email add, receivers add, time/date received, subject and senders IP add.
My problem is that i can only search a string in c# using indexes and i have to manually count the number of characters to format what i want. What i was thinking of doing is keeping to the indexes but instead of counting the characters use a function to count the "spaces" because for example regardless of how long the senders email add is there will always be "spaces" separating it.

Any ideas on how to implement this?

Here is my code so you can get a better understanding of what i'm trying to say.BTW the indexes used are specific to a header i used and i've attached a screenshot of the GUI. Thanks!!

namespace WindowsApplication3
{
    
    public partial class Form1 : Form
    {
        

       
        public Form1()
        {
            InitializeComponent();
            
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            to();
            from();
            ip();
            subject();
            rec();
          
        }

       
        private void ip()
        {
            string s = textBox1.Text;
            int i = s.IndexOf("X-Originating-IP"); 
            int t = i + 19;
            textb_ip.Text = s.Substring(t, 14);
        }

        private void from()
        {
            string s = textBox1.Text;
            int q = s.IndexOf("Return-Path");
            int w = q + 14;
            textb_from.Text = s.Substring(w, 22);
        
        }

        private void subject()
        {
            string s = textBox1.Text;
            int e = s.IndexOf("Subject:");
            int r = e + 9;
            textb_sub.Text = s.Substring(r, 4);
        }

        private void rec()

        {
            string s = textBox1.Text;
            int t = s.IndexOf("Received:");
            int y = t + 63;
            textb_rec.Text = s.Substring(y, 37);
        
        }

        private void to()

        {
            string s = textBox1.Text;
            int u = s.IndexOf("Delivered-To:");
            int o = u + 14;
            textb_to.Text = s.Substring(o, 33);
        
        }

    }
}

Well I would do that in completely different way... probably using regular expressions... but here's the way to count spaces in the provided text:

static public int spaces(string text)
        {
            char[] delimiters = { ' ' };
            text = text.Trim();
            string[] words = text.Split(delimiters);
            return words.Length - 1;
        }

hope it helps...

This article has been dead for over six months. Start a new discussion instead.