Hi all,

I need some help here,

The data in textbox looks like this:

M/z= 151.874862, P= 32.624691%, Rel. Int.= 78.220479%
M/z= 152.878217, P= 0.352860%, Rel. Int.= 0.846011%
M/z= 153.871912, P= 41.708632%, Rel. Int.= 100.000000%
M/z= 154.875267, P= 0.451109%, Rel. Int.= 1.081573%
M/z= 155.868962, P= 19.995706%, Rel. Int.= 47.941409%
M/z= 156.872317, P= 0.216268%, Rel. Int.= 0.518521%
M/z= 157.866012, P= 4.260544%, Rel. Int.= 10.215017%
M/z= 158.869367, P= 0.046081%, Rel. Int.= 0.110483%
M/z= 159.863062, P= 0.340427%, Rel. Int.= 0.816204%

I want to separate all the values (numerical values) of M/z into a textbox and values (numerical values) of Rel. Int. into another textbox.

I wrote a code like this as I'm in the initial stage of C#

foreach(string line in richTextBox1.Lines)
            {
                for (int i = 0; i < line.Length; i++)
                {
                    if(((line[i]>='A')  && (line[i]<='Z')) || ((line[i]>='a') && (line[i]<='z')) || (line[i] == '='))
                    {
                        textBox23.AppendText(line[i].ToString());
                    }
                    else if (((line[i] >= '0') && (line[i] <= '9')) || (line[i] == '.'))
                    {
                        textBox24.AppendText(line[i].ToString());
 
                    }
                    if (line[i] == ',')
                    {
                        textBox24.AppendText(Environment.NewLine);
                    }
                }
                textBox24.AppendText(Environment.NewLine);

            }

            foreach (string lines in textBox24.Lines)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    if (((lines[i] >= '0') && (lines[i] <= '9')) || (lines[i] == '.'))
                    {
                        textBox25.AppendText(lines[i].ToString());

                    }
                    if (lines[i] == '.' && lines[i+1] == '.') 
                    {
                        textBox24.AppendText(Environment.NewLine);
                    }
                }

But there are some errors here and the values are not separated into two textboxes here.
Please help me out.

Recommended Answers

All 6 Replies

I can think of two ways to quickly do this, one using String.Split() and the other using Regex.

Take a look at those and see what you can come up with.

With the help of String.Split(), you can achieve it easily. The way you are doing right now is very time consuming and costly, if the file is too big and heavy. Try by below code..

foreach(string line in richTextBox1.Lines)
{
     string [] strLineSplit = line.Split(','); // line is comma seperated, so split line by comma. In strLineSplit you will get array of string with having 3 elements(0,1,2).
     textBox24.AppendText(strLineSplit[0].SubString(5,strLineSplit [0].Length -5));
     textBox24.AppendText(Environment.NewLine);
}

Same thing for your other string you want to append in textbox with only numeric value.

That's all... I hope it will help to you..

Let us know if it will not work you..

Hi all,

I need some help here,

The data in textbox looks like this:

M/z= 151.874862, P= 32.624691%, Rel. Int.= 78.220479%
M/z= 152.878217, P= 0.352860%, Rel. Int.= 0.846011%
M/z= 153.871912, P= 41.708632%, Rel. Int.= 100.000000%
M/z= 154.875267, P= 0.451109%, Rel. Int.= 1.081573%
M/z= 155.868962, P= 19.995706%, Rel. Int.= 47.941409%
M/z= 156.872317, P= 0.216268%, Rel. Int.= 0.518521%
M/z= 157.866012, P= 4.260544%, Rel. Int.= 10.215017%
M/z= 158.869367, P= 0.046081%, Rel. Int.= 0.110483%
M/z= 159.863062, P= 0.340427%, Rel. Int.= 0.816204%

I want to separate all the values (numerical values) of M/z into a textbox and values (numerical values) of Rel. Int. into another textbox.

I wrote a code like this as I'm in the initial stage of C#

foreach(string line in richTextBox1.Lines)
            {
                for (int i = 0; i < line.Length; i++)
                {
                    if(((line[i]>='A')  && (line[i]<='Z')) || ((line[i]>='a') && (line[i]<='z')) || (line[i] == '='))
                    {
                        textBox23.AppendText(line[i].ToString());
                    }
                    else if (((line[i] >= '0') && (line[i] <= '9')) || (line[i] == '.'))
                    {
                        textBox24.AppendText(line[i].ToString());
 
                    }
                    if (line[i] == ',')
                    {
                        textBox24.AppendText(Environment.NewLine);
                    }
                }
                textBox24.AppendText(Environment.NewLine);

            }

            foreach (string lines in textBox24.Lines)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    if (((lines[i] >= '0') && (lines[i] <= '9')) || (lines[i] == '.'))
                    {
                        textBox25.AppendText(lines[i].ToString());

                    }
                    if (lines[i] == '.' && lines[i+1] == '.') 
                    {
                        textBox24.AppendText(Environment.NewLine);
                    }
                }

But there are some errors here and the values are not separated into two textboxes here.
Please help me out.

Hi Rohand,

Thanks for the reply,

It worked. I added a new line for the code and it looks like this

foreach (string line in richTextBox1.Lines)
                {
                    string[] strLineSplit = line.Split(','); // line is comma seperated, so split line by comma. In strLineSplit you will get array of string with having 3 elements(0,1,2).
                    textBox24.AppendText(strLineSplit[0].Substring(5, strLineSplit[0].Length - 5));
                    textBox23.AppendText(strLineSplit[2].Substring(10, strLineSplit[0].Length - 4));
                    textBox24.AppendText(Environment.NewLine);
                    textBox23.AppendText(Environment.NewLine);

                }

I also want the values of Rel.Int into another textbox. Here it is giving values but it is also giving some extra characters in it.

Is this correct what I'm doing to acheive my goal.

The text box data looks like this.

Chemical formula: C1 Cl4

M/z= 151.874862, P= 32.624691%, Rel. Int.= 78.220479%
M/z= 152.878217, P= 0.352860%, Rel. Int.= 0.846011%
M/z= 153.871912, P= 41.708632%, Rel. Int.= 100.000000%
M/z= 154.875267, P= 0.451109%, Rel. Int.= 1.081573%
M/z= 155.868962, P= 19.995706%, Rel. Int.= 47.941409%
M/z= 156.872317, P= 0.216268%, Rel. Int.= 0.518521%
M/z= 157.866012, P= 4.260544%, Rel. Int.= 10.215017%
M/z= 158.869367, P= 0.046081%, Rel. Int.= 0.110483%
M/z= 159.863062, P= 0.340427%, Rel. Int.= 0.816204%

Number of peaks calculated:              10
Number of peaks after binning:           10
Number of peaks above minimum intensity: 9

Covered Intensity: 100.000%

Minimum Mr difference: 0.993695 between 152.878217 and 153.871912
Computing time: 0 seconds.

With the help of String.Split(), you can achieve it easily. The way you are doing right now is very time consuming and costly, if the file is too big and heavy. Try by below code..

foreach(string line in richTextBox1.Lines)
{
     string [] strLineSplit = line.Split(','); // line is comma seperated, so split line by comma. In strLineSplit you will get array of string with having 3 elements(0,1,2).
     textBox24.AppendText(strLineSplit[0].SubString(5,strLineSplit [0].Length -5));
     textBox24.AppendText(Environment.NewLine);
}

Same thing for your other string you want to append in textbox with only numeric value.

That's all... I hope it will help to you..

Let us know if it will not work you..

If the textbox holds other data as shown here then i would definitely recommend the regex route.
A regular expression like M/z= (?<Mz>\d*?\.\d*?), P= (?<P>\d*?\.\d*?)\%, Rel\. Int\.= (?<Rel>\d*?\.\d*?)\% uses named capture groups to extract all three values into a single regex match.
You use Match.Groups["GroupName"] to read them back.

Yes definitely if your textbox contains data other than mentioned format ("M/z= 151.874862, P= 32.624691%, Rel. Int.= 78.220479%") then you must go with RegEx instead of string.Split().

Try by RegEx and let us know if you face any trouble...

Hi,

I will try that and will let you know about it.

thanks,
Yamini

Yes definitely if your textbox contains data other than mentioned format ("M/z= 151.874862, P= 32.624691%, Rel. Int.= 78.220479%") then you must go with RegEx instead of string.Split().

Try by RegEx and let us know if you face any trouble...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.