C#。如何编写可以在字符串中搜索char并返回出现次数的函数? [英] C#. How do I write a function that can search a char within a string and return the number of occurrences?

查看:69
本文介绍了C#。如何编写可以在字符串中搜索char并返回出现次数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我需要一个函数来搜索字符串并查找给定字符串中给定char的出现次数。你能帮我吗 ?谢谢。



我在下面尝试了这个代码,它只是给出了字符串中的所有字符..而且我的老师说这太复杂了我需要简化它。也许我应该定义一个字符串和一个字符并使用if else语句..我想不出其他的东西..所以请欢迎任何想法。



对不起,如果你不太了解英语是我的第二语言,我试着尽可能清楚地表达自己。



我尝试了什么:



  namespace  ConsoleApplication5 
{
class ex3
{
static void Main( string [] args)
{
string str = aabb12cc< /跨度>;
var count = CharacterCount.Count(str);
foreach var 字符 count)
{
Console.WriteLine( {0} - {1},character.Key,character.Value);
}
}

}
class CharacterCount
{
public static SortedDictionary< char, int = > 计数( string stringToCount)
{
SortedDictionary< char, int = > characterCount = new SortedDictionary< char, int = > ();
foreach var 字符 stringToCount)
{
int counter = 0 ;
characterCount.TryGetValue(character, out counter);
characterCount [character] = counter + 1 ;
}
return characterCount;
}
}
}

解决方案

可以简化代码的第一件事就是字符串是一个字符数组。例如,尝试使用以下代码:

  string  str =   aabb12cc; 
Console.WriteLine(str [ 3 ]);



话虽如此,你可以拥有循环遍历所有字符的单个循环,如果需要字符,则将计数器递增1。一个简单的循环可以是 for(C#Reference) [ ^ ]循环从0开始并在字符串结尾处结束。


Hi! I need a function who searches a string and find the number of occurrences of a given char within this given string. Can you help ? thank you.

I have tried this code below and it just gives all the chars within the string .. and my teacher said it was too complicated and I need to simplified it. And maybe I should define a string and a char and use a if else statement .. and I can't think of something else .. so please any ideas will be welcomed.

Sorry if you don't understand it well enough English is my second language and I tried to express myself as clearly as I can.

What I have tried:

namespace ConsoleApplication5
{
    class ex3
    {
        static void Main(string[] args)
        {
            string str = "aabb12cc";
            var count = CharacterCount.Count(str);
            foreach (var character in count)
            {
                Console.WriteLine("{0}-{1}", character.Key, character.Value);
            }
        }

    }
    class CharacterCount
    {
        public static SortedDictionary<char, int=""> Count(string stringToCount)
        {
            SortedDictionary<char, int=""> characterCount = new SortedDictionary<char, int="">();
            foreach (var character in stringToCount)
            {
                int counter = 0;
                characterCount.TryGetValue(character, out counter);
                characterCount[character] = counter + 1;
            }
            return characterCount;
        }
    }
}

解决方案

First thing that could simplify your code is the fact that the string is an array of chars. For example try what happens with the following code

string str = "aabb12cc";
Console.WriteLine(str[3]);


Having that said, you could have a single loop looping through all the characters and if the character is desired one, then increment the counter by one. A simple loop could be a for (C# Reference)[^] loop starting from 0 and ending at the end of the string.


这篇关于C#。如何编写可以在字符串中搜索char并返回出现次数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆