Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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

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,我想根据字母、数字和C#中的特殊字符的数量来计算字符串的摘要。例如: 字符串abc123$%应具有类似于A3D3S2的摘要(表示3个字母、3个数字和2个特殊字符) a34=$@应该有类似于A1D2S3的摘要(即1个字母、2个数字和3个特殊字符) a3b$s应具有类似于A1D1A1S1A1的摘要(即1个字母、1个数字、1个字母、1个特殊字符、1个字母) 有谁能指导我如何编写一个能够快速执行上述任务的算法?我想如果我一个字符一个字符地搜索字符串,那么它将花费相当多的时间。我有一个很大的字符串数据集。

我想根据字母、数字和C#中的特殊字符的数量来计算字符串的摘要。例如:

字符串abc123$%应具有类似于A3D3S2的摘要(表示3个字母、3个数字和2个特殊字符)

a34=$@应该有类似于A1D2S3的摘要(即1个字母、2个数字和3个特殊字符)

a3b$s应具有类似于A1D1A1S1A1的摘要(即1个字母、1个数字、1个字母、1个特殊字符、1个字母)


有谁能指导我如何编写一个能够快速执行上述任务的算法?我想如果我一个字符一个字符地搜索字符串,那么它将花费相当多的时间。我有一个很大的字符串数据集。

使用
FOR
循环遍历每个字符。如果字符在
a-z
a-z
范围内,则它是一个字母表。如果在
0-9
范围内,则为数字else特殊字符

代码

string inputStr = "a3b$s";
string outputStr = string.Empty;
char firstChar = Convert.ToChar(inputStr.Substring(0, 1));
outputStr = char.IsLetter(firstChar) ? "A1" : char.IsDigit(firstChar) ? "D1" : "S1";
for (int i = 1; i < inputStr.Length; i++)
{
    char nextChar = char.IsLetter(inputStr[i]) ? 'A' : 
                       char.IsDigit(inputStr[i]) ? 'D' : 'S';
    char prevChar = Convert.ToChar(outputStr.Substring(outputStr.Length - 2, 1));
    if (nextChar == prevChar)
    {
        int lastDig = Convert.ToInt32(outputStr.Substring(outputStr.Length - 1, 1));
        outputStr = outputStr.Substring(0, outputStr.Length - 1) + 
                         (lastDig + 1).ToString();
    }
    else
        outputStr += nextChar.ToString() + "1";
}
Console.WriteLine(outputStr.ToString());

使用Linq,您可以这样做:

string myinput = "abc123$%";
            int letter =0 , digit = 0, specialCharacter = 0;
            myinput.ToCharArray().ToList().ForEach(x =>
            {
                letter = Char.IsLetter(x) ? ++letter : letter;
                digit = Char.IsDigit(x) ? ++digit : digit;
                specialCharacter = !Char.IsLetterOrDigit(x) ? 
                                  ++specialCharacter : specialCharacter;
            });
            string formattedVal = String.Format("A{0}D{1}S{2}", letter, digit, 
                                                       specialCharacter);
您可以通过以下方式直接在Linq ForEach中使用数组,而无需转换为列表:

Array.ForEach(myinput.ToCharArray(), x =>
            {
                letter = Char.IsLetter(x) ? ++letter : letter;
                digit = Char.IsDigit(x) ? ++digit : digit;
                specialCharacter = !Char.IsLetterOrDigit(x) ? ++specialCharacter : specialCharacter;
            });
            string formattedVal = String.Format("A{0}D{1}S{2}", letter, digit, specialCharacter);
这应该起作用:

    string s = "a3b$s";
    char etype = 'X'; //current character's type
    char etypeinit = 'X'; //tracker variable - holds type of last character
    string str = "";
    int count = 1;
    foreach(char c in s)
    {
        //Use this block of conditionals to assign type for current character
        if(char.IsLetter(c))
        {
            etype = 'A';
        }
        else if(char.IsDigit(c))
        {
            etype = 'D';
        }
        else
        {
            etype = 'S';
        }

        //This is a different type of character compared to the previous one
        if(etypeinit != etype)
        {
            str += string.Format("{0}{1}",etype,count); //Build the string
            count = 1; //Reset count
        }
        else
        {
            count++; //Increment because this is the same type as previous one
        }
        etypeinit = etype; //Set tracker variable to type of current character

    }

    Console.WriteLine(str);
这项工作:

    static string GetSummary(string input)
    {
        var sb = new StringBuilder();

        string prevMode = "";
        string curMode = "";
        int sameModeCount = 0;

        for (int i = 0; i <= input.Length; ++i)
        {
            if (i < input.Length)
            {
                char c = input[i];
                if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z')
                {
                    curMode = "A";
                }
                else if ('0' <= c && c <= '9')
                {
                    curMode = "D";
                }
                else
                {
                    curMode = "S";
                }
            }
            else
            {
                curMode = "";
            }

            if (curMode != prevMode && prevMode != "")
            {
                sb.Append(prevMode);
                sb.Append(sameModeCount);

                sameModeCount = 0;
            }

            prevMode = curMode;
            ++sameModeCount;
        }

        return sb.ToString();
    }
结果:

A3D3S2
A1D2S3
A1D1A1S1A1

有点晚,有点复杂,但能够根据问题中给定的输入产生所有预期输出,请看:

string inputString = "abc123$%ab12";

var results = inputString.Select(x => char.IsLetter(x) ? 'A' :
                                     char.IsDigit(x) ? 'D' : 'S');
StringBuilder outPutBuilder = new StringBuilder();
char previousChar = results.First();
int charCount = 0;
foreach (var item in results)
{
    switch (item)
    {
        case 'A':
            if (previousChar == 'A')
            {
                charCount++;
            }
            else
            {
                outPutBuilder.Append(previousChar.ToString() + charCount);
                charCount = 1;
            }
            break;
        case 'D':
            if (previousChar == 'D')               
                charCount++;               
            else
            {
                outPutBuilder.Append(previousChar.ToString() + charCount);
                charCount = 1;
            }
            break;
        default:
            if (previousChar == 'S')
               charCount++;               
            else
            {
                outPutBuilder.Append(previousChar.ToString() + charCount);
                charCount = 1;
            }
            break;
    }
    previousChar = item;
}
outPutBuilder.Append(previousChar.ToString() + charCount);

但我必须保持摘要字符串的顺序,我已经编辑了我的问题,并添加了另一个示例,以便根据问题进行澄清。当我将
a3b$s
作为输入时,它应该将
a1d1s1a1
作为输出,但您的给出的是
A3D1S1
哪一个是正确的?字符串的顺序也应该是正确的maintained@WiXXeY:我已经更新了我的答案。我没有注意到最后一个场景。抱歉。与@Ullas的回答相同的问题-这给出了最后一个测试用例的A3D1S1@shree.pat18 a34b$s的输出是A1D1A2S1A1,但它应该是A1D2A1S1A1问题是:“aabfdnas”的输出是A1,“asd233”的输出是A1D3,233ad的输出是D1A2。第一个元素的值始终为1。
A3D3S2
A1D2S3
A1D1A1S1A1
string inputString = "abc123$%ab12";

var results = inputString.Select(x => char.IsLetter(x) ? 'A' :
                                     char.IsDigit(x) ? 'D' : 'S');
StringBuilder outPutBuilder = new StringBuilder();
char previousChar = results.First();
int charCount = 0;
foreach (var item in results)
{
    switch (item)
    {
        case 'A':
            if (previousChar == 'A')
            {
                charCount++;
            }
            else
            {
                outPutBuilder.Append(previousChar.ToString() + charCount);
                charCount = 1;
            }
            break;
        case 'D':
            if (previousChar == 'D')               
                charCount++;               
            else
            {
                outPutBuilder.Append(previousChar.ToString() + charCount);
                charCount = 1;
            }
            break;
        default:
            if (previousChar == 'S')
               charCount++;               
            else
            {
                outPutBuilder.Append(previousChar.ToString() + charCount);
                charCount = 1;
            }
            break;
    }
    previousChar = item;
}
outPutBuilder.Append(previousChar.ToString() + charCount);