Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#字符串操作请求_C#_String - Fatal编程技术网

C#字符串操作请求

C#字符串操作请求,c#,string,C#,String,我似乎找不到一个“好”的方法来用字符串来完成任务 假设我有一个字符串,其值为“Take Chocolate” 我使用.Contains方法检测该字符串中是否包含字符串“cola”,并且它确实包含“cola”。我想要一种让程序在“cola”中得到“c”之前的字符和“a”之后的字符的方法,以检查字符是否都是空的。有什么办法解决这个问题吗?任何提示或想法都会很棒 另外,我正在制作一个非常广泛的基于文本的冒险游戏。这非常简单。当您检查.Contains(“可乐”)时,请改为检查.Contains(“可乐

我似乎找不到一个“好”的方法来用字符串来完成任务

假设我有一个字符串,其值为“Take Chocolate” 我使用.Contains方法检测该字符串中是否包含字符串“cola”,并且它确实包含“cola”。我想要一种让程序在“cola”中得到“c”之前的字符和“a”之后的字符的方法,以检查字符是否都是空的。有什么办法解决这个问题吗?任何提示或想法都会很棒


另外,我正在制作一个非常广泛的基于文本的冒险游戏。

这非常简单。当您检查.Contains(“可乐”)时,请改为检查.Contains(“可乐”)。希望这正是你想要做的。

这很简单。当您检查.Contains(“可乐”)时,请改为检查.Contains(“可乐”)。希望这正是您想要做的。

您所描述的内容可以轻松完成。然而,一种更简单的方法是将字符串按空格分割,然后分析播放器命令的每个部分

下面是一个高度简化的示例,但它让人明白了这一点:

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]);
    }
}

像这样将文本命令分解成一个简单的树,是基于文本的冒险游戏最早出现的核心。

您所描述的内容可以很容易地完成。然而,一种更简单的方法是将字符串按空格分割,然后分析播放器命令的每个部分

    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);

    }
下面是一个高度简化的示例,但它让人明白了这一点:

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]);
    }
}
将文本命令分解成这样一个简单的树是基于文本的冒险游戏最早的核心。

static void Main(string[]args)
    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);

    }
{ string s=“吃巧克力”; string searchString=“cola”; int beginIndex=0; int-endIndex=0; 如果包含(搜索字符串)) { beginIndex=s.IndexOf(searchString,0,s.Length); endIndex=beginIndex+searchString.Length; } 如果(endIndex0) Console.WriteLine(s[beginIndex-1]); 其他的 Console.WriteLine(“在“+searchString”之前没有字符); }
这里有一个小程序在做这个。希望有帮助。

静态void Main(string[]args)
{
string s=“吃巧克力”;
string searchString=“cola”;
int beginIndex=0;
int-endIndex=0;
如果包含(搜索字符串))
{
beginIndex=s.IndexOf(searchString,0,s.Length);
endIndex=beginIndex+searchString.Length;
}
如果(endIndex0)
Console.WriteLine(s[beginIndex-1]);
其他的
Console.WriteLine(“在“+searchString”之前没有字符);
}
这里有一个小程序在做这个。希望有帮助。

如果您的任务是“确定短语是否包含单词”,您可以将短语拆分为单词,然后检查每个单词

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);
如果您的任务是“确定短语是否包含单词”,您可以在中将短语拆分为单词,然后检查每个单词

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);

它在“cola”中返回“c”之前的字符,在句子中返回“a”之后的字符

   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;
    }
   }

它在“cola”中返回“c”之前的字符,在句子中返回“a”之后的字符

   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;
    }
   }

可能性重复可能性重复可能性重复几率是OP也愿意在字符串的开头或结尾接受“cola”,在这种情况下这不起作用。可能性是OP也愿意在字符串的开头或结尾接受“cola”,在这种情况下这不起作用。我忘了indexof方法,我当然可以用this@DurpBurger可能会有一些小错误,我是用心写的。检查一下。#我的出生名我可能会接受使用IndexOf的概念,它的其余部分似乎与我的目的无关,但仍然是很好的代码。@DurpBurger是的,我写的其他东西只有一个工作示例:)祝你的项目好运!我忘了indexof方法,我肯定可以使用this@DurpBurger可能会有一些小错误,我是用心写的。检查一下。#我的出生名我可能会接受使用IndexOf的概念,它的其余部分似乎与我的目的无关,但仍然是很好的代码。@DurpBurger是的,我写的其他东西只有一个工作示例:)祝你的项目好运!