Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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#如果字符串中重复出现字符,则返回true或false的方法/函数_C# - Fatal编程技术网

C#如果字符串中重复出现字符,则返回true或false的方法/函数

C#如果字符串中重复出现字符,则返回true或false的方法/函数,c#,C#,我需要使用C#编写一个方法/函数,它接受两个参数:一个字符串和一个字符。如果字符串中的每个字符实例的左边或右边都有另一个字符实例,则函数应返回true。换句话说,仅当字符仅成对出现在字符串中时,函数才应返回true。我似乎不明白 示例: Manipulation("abcdeefghi", 'e') -> true Manipulation("abcdeeefghi", 'e') -> false 目前我有: public bool Manipulation(string strP

我需要使用C#编写一个方法/函数,它接受两个参数:一个字符串和一个字符。如果字符串中的每个字符实例的左边或右边都有另一个字符实例,则函数应返回true。换句话说,仅当字符仅成对出现在字符串中时,函数才应返回true。我似乎不明白

示例:

Manipulation("abcdeefghi", 'e') -> true
Manipulation("abcdeeefghi", 'e') -> false
目前我有:

public bool Manipulation(string strParam, char[] charParam)
{
    if (strParam.Contains(charParam))
    {
        return true;                
    }
    return false;
}

我已经知道这不起作用,但如果我能在字符串中至少找到一次,我至少会尝试返回true。

查看它的一种方法是遍历字符串一次,只是检查是否出现一次字符:注意,这个答案要求
字符
正好出现两次,例如:

IsPairOccurence("abcdeefghi", 'e') -> true
IsPairOccurence("abcdeeefghi", 'e') -> false
如@aditya的回答所示,检查它是否至少出现两次更容易

private bool IsPairOccurence(string s, char c) {
    int occurence = 0; // the number of consecutive occurences
    for (int i = 0; i < s.Length; i++) {
        if (s[i] == c) { // If you encounter the character
            occurrence++; // Increase the counter
        } else { // If another character occurs
            if (occurence == 2) {
            // Check if the number of consecutive occurences was exactly 2
                return true;
            }
            // If not, reset the counter
            occurence = 0;
        }
    }
    return occurence == 2;
}

现在,
ispairocurrence(s,c)
只是
doesocurntimes(s,c,2)
这应该对你有用,但正如@ThreeFx在评论中建议的那样,你需要提及它是否仅适用于成对或三联体等等

然而,答案假设后一种情况:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackQuestionAnswersCS
{
    class Program
    {
        static void Main(string[] args)
        {
            if (checkContains("HelloWorld", 'o')) Console.WriteLine("Contains Character");
            else Console.WriteLine("Does not contain character");
            Console.ReadLine();
        }

        static bool checkContains(string input, char character)
        {
            for (int i = 0; i < input.Length - 1; i++)
            {
                if (input[i] == character && input[i+1] == character) return true;
            }
            return false;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间StackQuestionAnswersCS
{
班级计划
{
静态void Main(字符串[]参数)
{
if(checkContains(“HelloWorld”,“o”))Console.WriteLine(“Contains Character”);
else Console.WriteLine(“不包含字符”);
Console.ReadLine();
}
静态bool checkContains(字符串输入,字符)
{
for(int i=0;i
两个参数都应该是字符串(而不是char[])。@jdweng No.一个参数应该是char,一个参数应该是字符串。f(“abcdeeefghi”,e)会产生什么结果?它只适用于成对的还是三胞胎等?你不需要计数,只需返回即可true@Steve是的,他的代码做得很好(正如您可以通过返回类型
bool
看到的)。您必须以某种方式计数字符,以确定其中一个字符是否出现两次。如果您的代码在
checkContains(“abcdeeefghi”,“e”)
上返回
true
,Steve进行了更正。我尝试了一种不同的方法(最初是更复杂的方法,因此是计数),这更有效。减少比较的次数。@ThreeFx,是的,因为问题中不清楚,我假设字符串中的字符同时出现一个或多个。当将字符发送到此函数时,它应该是这样的吗?string strParam=txtString.Text;char[]charParam=txtChar.Text.ToCharArray();bool-answer=ispairoccurrence(strParam,charParam);不幸的是,对于(abcdeeghe,e)@Mabbott不,您必须使用
char
来调用它,而不是
char[]
。因此,选择一个字符并传递它,例如
charParam
的第一个
char
ispairoccurrence(strParam,charParam[0])
。根据你的问题,这是唯一合理的解释。@ThreeFx谢谢@再见,为什么?期望值是多少?返回的
true
是正确的解释。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackQuestionAnswersCS
{
    class Program
    {
        static void Main(string[] args)
        {
            if (checkContains("HelloWorld", 'o')) Console.WriteLine("Contains Character");
            else Console.WriteLine("Does not contain character");
            Console.ReadLine();
        }

        static bool checkContains(string input, char character)
        {
            for (int i = 0; i < input.Length - 1; i++)
            {
                if (input[i] == character && input[i+1] == character) return true;
            }
            return false;
        }
    }
}
private static bool Manipulation(string p, char c)
{
   return p.Contains(c.ToString() + c.ToString()) && !p.Replace(c.ToString() + c.ToString(), "").Contains(c);
}