Benyny
Benyny

Reputation: 11

The problem of working with strings and numbers from a file displayed in a RichTextBox.C#

i'm newbie(and English sorry for that) in c# programming and have problem of working with string and numbers in richtextbox. I can't return numbers from strings to do some operation with digits and display result in new RichTextBox.Strings are getting from files.

My Strings(more than 2)

Exam 1: 10 11 12 99 88 14;

Exam 2: 60 41 72 93 55 44;

and "how do i need dislpay in new richtextbox"

Exam 1: (Average);

Exam 2: (Average);

Upvotes: 0

Views: 71

Answers (1)

yassinMi
yassinMi

Reputation: 737

Try the following code.

//the input string MUST look like: "Exam 1: 10 11 12 99 88 14;"
int sep_index = input.IndexOf(':');
int terminator_index = input.IndexOf(';');
int exam_number = int.Parse( input.Substring(5,sep_index -5));
var data = input.Substring(sep_index +1,terminator_index-sep_index-1).Trim().Split(' ')
             .Select(s=>int.Parse(s));
var average = data.Average();

You can start from there and make it more robust

Upvotes: 2

Related Questions