C# 拆分字符串并乘以8

C# 拆分字符串并乘以8,c#,C#,我在这方面遇到了麻烦。用户需要输入一个字符串,然后我需要对该字符串进行计数并乘以相同的字符串。例如,如果用户输入字符串,快速棕色狐狸跳过懒惰的狗 输出应该是这样的,即=22%quick=11%brown=11%fox=11%jumps=11%over=11%lazy=11%dog=11% 这是我的密码 string phrase = "The quick brown fox jumps over the lazy dog"; string[] arr1 = phrase.Spl

我在这方面遇到了麻烦。用户需要输入一个字符串,然后我需要对该字符串进行计数并乘以相同的字符串。例如,如果用户输入字符串,快速棕色狐狸跳过懒惰的狗
输出应该是这样的,即=22%quick=11%brown=11%fox=11%jumps=11%over=11%lazy=11%dog=11%

这是我的密码

 string phrase = "The quick brown fox jumps over the lazy dog";
        string[] arr1 = phrase.Split(' ');


        for (int a = 0; a < arr1.Length; a++)
        {
            Console.WriteLine(arr1[a]);
        }



        Console.ReadKey();
string短语=“敏捷的棕色狐狸跳过了懒狗”;
字符串[]arr1=短语.Split(“”);
for(int a=0;a

该值为22%,使用该公式计算,2/9*100。2因为“the”用了两次,除以9是因为字符串中有9个单词。我试图比较每个字符串,以确定它们是否相同,但无法这样做

我会使用两个列表

List<String> words  = new List<String>();
List<int> weight = new List<int>();
List words=new List();
列表权重=新列表();
在遍历字符串时,只向单词列表添加唯一的单词,然后权重列表的相应索引增加1

完成后,可以将每个权重值除以字符串的长度[]

要获取唯一值,您可以通过执行以下操作:

  • 自动将第一个字符串添加到列表中
  • 对于其后的每个字符串,执行words.Contains(字符串[x])
  • 如果它不包含它,则添加它
  • 如果确实包含它,则执行words.indexOf(字符串[x])
  • 然后在权重列表中增加相应的索引

我会使用两个列表

List<String> words  = new List<String>();
List<int> weight = new List<int>();
List words=new List();
列表权重=新列表();
在遍历字符串时,只向单词列表添加唯一的单词,然后权重列表的相应索引增加1

完成后,可以将每个权重值除以字符串的长度[]

要获取唯一值,您可以通过执行以下操作:

  • 自动将第一个字符串添加到列表中
  • 对于其后的每个字符串,执行words.Contains(字符串[x])
  • 如果它不包含它,则添加它
  • 如果确实包含它,则执行words.indexOf(字符串[x])
  • 然后在权重列表中增加相应的索引

    • 强制性LINQ版本:

      string phrase = "The quick brown fox jumps over the lazy dog";
      string[] words = phrase.Split(' ');
      var wc = from word in words
               group word by word.ToLowerInvariant() into g
               select new {Word = g.Key, Freq = (float)g.Count() / words.Length * 100};
      

      强制性LINQ版本:

      string phrase = "The quick brown fox jumps over the lazy dog";
      string[] words = phrase.Split(' ');
      var wc = from word in words
               group word by word.ToLowerInvariant() into g
               select new {Word = g.Key, Freq = (float)g.Count() / words.Length * 100};
      

      尽量少使用
      LINQ

              string phrase = "The quick brown fox jumps over the lazy dog";
              string[] words = phrase.ToLower().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
      
              var distinct_words = words.Distinct().ToArray();
              foreach (string word in distinct_words)
              {
                  int count = words.Count(wrd => wrd == word);
                  Console.WriteLine("{0} = {1} % ", word, count * 100 / words.Length);
              }
      


      尽量少使用
      LINQ

              string phrase = "The quick brown fox jumps over the lazy dog";
              string[] words = phrase.ToLower().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
      
              var distinct_words = words.Distinct().ToArray();
              foreach (string word in distinct_words)
              {
                  int count = words.Count(wrd => wrd == word);
                  Console.WriteLine("{0} = {1} % ", word, count * 100 / words.Length);
              }
      

      你可以试试这个:

       var  yourarr = phrase.Split(' ').GroupBy(word => word.ToUpper()).Select(w => ((w.Count()*100/ phrase.Split(' ').Distinct().Count())).ToString()+"%");
      
      你可以试试这个:

       var  yourarr = phrase.Split(' ').GroupBy(word => word.ToUpper()).Select(w => ((w.Count()*100/ phrase.Split(' ').Distinct().Count())).ToString()+"%");
      

      到目前为止,您已经编写了拆分字符串的代码。您仍然需要编写代码来处理这些单词。这是怎么回事?你可以使用
      字典
      ,文档:,或者你可以在你的数组上使用
      GroupBy
      linq方法。所以你需要一个单词的直方图。可能会将标题更新为“如何从短语中获取单词的直方图”。到目前为止,您已经编写了拆分字符串的代码。您仍然需要编写代码来处理这些单词。这是怎么回事?你可以使用
      字典
      ,文档:,或者你可以在你的数组上使用
      GroupBy
      linq方法。所以你需要一个单词的直方图。可能会将标题更新为“如何从短语中获取单词直方图”。谢谢,希望它对你有用。如果是,请记得投票并选择作为答案。祝你好运谢谢,希望对你有用。如果是,请记得投票并选择作为答案。祝你好运Dezza,如果您不熟悉LINQ,那么请检查它,因为afrischke的解决方案很好而且简单,而且使用LINQ使编码快速而简单。Dezza,如果您不熟悉LINQ,那么请检查它,因为afrischke的解决方案很好而且简单,另外,因为使用LINQ使编码变得快速和简单。就像@afrisdhke.Oh一样。和@afrisdhke一样。