1 Replies - 4869 Views - Last Post: 19 January 2008 - 01:41 PM Rate Topic: -----

#1 kunaltilak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 08

c# split string problem

Post icon  Posted 19 January 2008 - 05:38 AM

hello sir,
please help me out for a asp.net(c#) problem.
actually i want to make an online test series.
for that i want to paste each question with options into rich text box.
each question has 4 options(named: a. , b. , c. , d. ).
like this:
f is defined on [-1, 1] by
a. f is continuous if and only if a > 0
b. f(0) exists if and only if a > 1
c. f is bounded if and only if a  1 + c
d. none of these
a. , b. , c. ,d. are the options

i want to split these above question as:

f is defined on [-1, 1] by
f is continuous if and only if a > 0
f(0) exists if and only if a > 1
f is bounded if and only if a  1 + c
none of these

i think you clearly understand my problem.
please help me out.

my code is:
using System.Text.RegularExpressions;

string[] abc = Regex.Split(finalString, "( a. | b. |c. |d. )");

foreach (string match in abc)

string FinalSplitString= match;


where final string is the string which i mention above.
My code is not working fine, suggest me some code so that I can access 5 string elements(1 question and 4 options)

please help me.
thanking you.

with regards,

Is This A Good Question/Topic? 0
  • +

Replies To: c# split string problem

#2 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: c# split string problem

Posted 19 January 2008 - 01:41 PM

i don't think the approach you are taking in your problem is adequate.
first separate the questions and answers (putt them in different arrays) then you can code the logic to retrieve the options for that question using the index of the question in it's array .i guess you wonna see some code huh, ok then
using System;
class QuestionAndAnswersIndex //long huh ? couldn't think of anything shorter
	const int MaxDisplayStrings = 5;
	string[] Questions;//initialise the arrays (questions and answers) with your preferred values
	string[] Answers;
	string[] DisplayInRichTextBox = new string[MaxDisplayStrings];// create a method to diplay this array in your RichTextBox

	public void FillDisplayArray(string question)
		int questionIndex = Array.BinarySearch(Questions, question.Trim());
		if (questionIndex >= 0 || questionIndex < Questions.Length)// check if questionIndex is out of range 
			DisplayInRichTextBox[0] = Questions[questionIndex];
			int AnswersIndex = 4 * questionIndex;//AnswerIndex points to the first appropriate answer for the question 
			for (int i = 1; i < MaxDisplayStrings; ++i)//(done the math already)if you sequencially load your arrays
				DisplayInRichTextBox[i] = Answers[AnswersIndex];
			Console.WriteLine("Sorry,Question does not exist in the Array ");

if i understood your problem ,this should be ok. :)

This post has been edited by gogole: 21 January 2008 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1