Reputation: 15
I can't seem to find an 'good' way to go about doing a task with a string
say if I have a string, whose value is "Take Chocolate" and I detect if that string has the string "cola", in it, using the .Contains method, and it does contain "cola" in it. I want a way for the program get the character before the 'c' in "cola" and the character after the 'a', to check if the characters are both empty spaces. Any ideas how to tackle this problem? Any tips or ideas would be fantastic!
P.S. I am making a quite extensive text-based adventure game.
Upvotes: 0
Views: 144
Reputation: 136
it return character before the 'c' in "cola" and the character after the 'a' in sentence
Class6 c = new Class6();
var t = s.ToLower().Replace("cola", "##").Split(new char[] { ',', '.', ' ' }).Where(h => h.Contains("##")).Select(h => c.getFistAndLast(h));
public class class6 {
public string getFistAndLast(string word)
{
string[] words = word.Split(new string[] { "##" }, StringSplitOptions.None);
string h = "";
if (words[0].Trim() != "")
h = words[0].Remove(0, words[0].Length - 1)+",";
if (words[1].Trim() != "")
h += words[1][0];
h += " ";
return h;
}
}
Upvotes: 0
Reputation: 148980
What you're describing could easily be done with regular expressions. However, an easier method is just to split the string by spaces, then analyzing each part of the player's command.
Here's a highly simplified example, but it gets the idea across:
var playerCommand = "Take Chocolate";
var parts = playerCommand.Split();
// NOTE: use StringComparison.CurrentCultureIgnoreCase for case insensitive comparison
if (parts[0] == "Take")
{
if (parts[1] == "Chocolate")
{
DoTakeChocolate();
}
else if (parts[1] == "Cola")
{
DoTakeCola();
}
else
{
Console.WriteLine("There's no {0} here for you to take", parts[1]);
}
}
Breaking down text commands into a simple tree like this has been at the heart of text-based adventure games since their earliest days.
Upvotes: 3
Reputation: 18127
static void Main(string[] args)
{
string s = "Take Chocolate";
string searchString = "cola";
int beginIndex = 0;
int endIndex = 0;
if (s.Contains(searchString))
{
beginIndex = s.IndexOf(searchString, 0, s.Length);
endIndex = beginIndex + searchString.Length;
}
if(endIndex < s.Length)
{
Console.WriteLine(s[endIndex + 1]);
}
else
{
Console.WriteLine("There is no character after" + searchString);
}
if(beginIndex > 0)
Console.WriteLine(s[beginIndex - 1]);
else
Console.WriteLine("There is no character before" + searchString);
}
Here is little program doing this. Hope it helps.
Upvotes: 1
Reputation: 3185
If your task is "determine if the phrase contains word", you can split the phrase into words in then check each of them,
char[] delimiters = [',','.',' ']; // all delimiters that are not letters
var phrase = "Take Chocolate, and,cola";
var containsCola = phrase.split(delimiters).Any(x => x.Equals("cola", StringComparison.OrdinalIgnoreCase);
Upvotes: 0
Reputation: 183
This is very simple to do. When you are checking for .Contains("cola"), instead check for .Contains(" cola "). Hope this is exactly what you want to do.
Upvotes: 2