Help splitting up and processing Textbox data .. for each and unknownnumbe of tokens ??

  • Thread starter Thread starter jc
  • Start date Start date
J

jc

Hello.. C# noob here needing some clue on how to do this in C#.

I have a giant textbox that will contain data that looks like this.

===
Question; Answer1;Answer2; AnswerN...
Are you a US citizen; Yes; No
When was the last time visited a Doctor; Prior to 2000; Prior to 2005;
In the Last 3 months
==

Columns delimited by semicolons
Rows delimited by new line.

For each row. The first column is the question, subsiquent columns are
anwsers. A question can have an unlimted number of answers.


I need to execute this code (substituting the data) for every line,
but don't know how many answers a row might have.

StringCollection strQuestions = new StringCollection();
string strQuestion = "Question";
strQuestions.Add("Answer1);
strQuestions.Add("Answer2);
strQuestions.Add("AnswerN);
objSurvey.Fields.Add(strQuestion, SPFieldType.Choice,
true, false, strQuestions);

What's the best way to approach this?

Thank you for any help or information.
 
look at this:

string somestring = "bla;bla;bla;";//in your case, mytextbox.Text or some
such thing.
string[] splitter = new string[1]{";"};//creating an array with only one
element.
string[] mystringarray =
somestring.Split(splitter,StringSplitOptions.None);//This creates an array
that is based on how many elements the string is split into.
That gives you an array of whatever you split it by, in the above example it
is with a semicolon. You will have to find what the code is for a character
return and do your first split with that. Then with the resulting array you
will have to do a split of each element using your semicolons. then you
will have to throw the first result into the question string and then create
a loop while you iterate through the remaining elements of the current array
to read the answers.

Good luck!
 
look at this:

string somestring = "bla;bla;bla;";//in your case, mytextbox.Text or some
such thing.
string[] splitter = new string[1]{";"};//creating an array with only one
element.
string[] mystringarray =
somestring.Split(splitter,StringSplitOptions.None);//This creates an array
that is based on how many elements the string is split into.

The extra array is unnecessary. All that is needed is:

string questionAndAnswers =
"Question;Answer1;Answer2;Answer3;Answer4";
string[] mystringarray = questionAndAnswers .Split(';');

mystringarray now contains the questions and all the answers

Chris
 
[snip]

[top posting fixed]
string[] mystringarray =
somestring.Split(splitter,StringSplitOptions.None);//This creates an array
that is based on how many elements the string is split into.

Note that this will fail an answer may contain an embedded semicolon.
In that case, you'll need to write a state parser.
 
Back
Top