Jump to content
  • Advertisement
Sign in to follow this  
phil05

C# string/switch question

This topic is 5042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just got into C# and I can't shut this book. I really love the langauge. If I can get this working, the better :) I need to break the buffer into single words and look for them in a switch. Currently, it just allows you to write one word and it looks for it through the cases. This works, but it'd be fun if I could break up the sentence. I have no purpose for this experiment, but I just wondered if it was possible. Thanks!
		string buffer = "";
		
		while (exit != true)
		{
			Console.Write(": ");
			buffer = Console.ReadLine();
			
			switch (buffer)
			{
					case "sword": Console.WriteLine("Yes, I have one!");
								  break;
				    case "/exit": exit = true;
				    			  break;
			}
				
			
		}	

Share this post


Link to post
Share on other sites
Advertisement
You can do it by using whatever String functions in C# to split up a "sentence" into multiple words and store each in a vector for example. Then loop your code that you have now (if C# accepts Strings as switches). Notice that instead of "buffer" you have to use the current single-word element that you have extracted from the "sentence".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!