DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   C# Code Question (http://www.daniweb.com/forums/thread52117.html)

knightrider2006 Aug 9th, 2006 11:07 am
C# Code Question
 
Hi!
I'm a beginner C# programmer, and our instructor has asked me to create a verification system for our web site so that a person has to complete a puzzle before they can fill out a form to submit comments on the site, etc.
What he wants us to do is to have a random sentence displayed such as "The cow jumped over the moon" or any sentence we want. I've already done this part.
But then, he wants us to make it so that the person must type in the fifth word in the sentence to complete the verification process.
My question is.. If you have a sentence like the one listed above, what code would one use to return just the fifth word in that sentence? The sentence is a string.
Hope someone can help.
From KN

plazmo Aug 9th, 2006 1:34 pm
Re: C# Code Question
 
I think an easy way to do this is get the index of the 4th and 5th space. then get the substring between them.

knightrider2006 Aug 9th, 2006 2:28 pm
Re: C# Code Question
 
Hmmm..Not really sure how that will work exactly.. An example might be useful to look at so I can see what's happening, then try to take that and translate it for my own project. All I want to get is that fifth word

Lord Soth Aug 12th, 2006 4:20 am
Re: C# Code Question
 
Hi,

You can use String.Split(" ") to get an array of words of your string, this way you can also know the number of words on that string. (if you plan to use multiple possible sentences)

Loren Soth

knightrider2006 Aug 14th, 2006 9:12 am
Re: C# Code Question
 
Hey!
Well, I did the string.split to create an array of words. Now how do I take that array of words and get the fourth word out of it and return it as a string?

plazmo Aug 15th, 2006 12:00 pm
Re: C# Code Question
 
yeah i like the split idea better.

string[] words = string.split(" ");
fifth word =words[4]


All times are GMT -4. The time now is 10:54 am.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC