计算字符串生成器中的唯一字数(不是字母......单词)) [英] Count the unique number of words inside a string builder(Not letters...Words))

查看:90
本文介绍了计算字符串生成器中的唯一字数(不是字母......单词))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的sttring bulilder字符串将是这样的:



StringBuilder sb = new StringBuilder();

sb.Append(hi朋友嗨);



i想要检查一下hi中的数量

所以我的最终输出会是这样的。



数量是2.



怎么做。



谢谢你

SUBIN

my sttring bulilder string would be like this:

StringBuilder sb = new StringBuilder();
sb.Append("hi friends hi");

i want to check the count of "hi" in the sb
so my final output would be like this.

Count is 2.

how to do that.

thank u
SUBIN

推荐答案

1。你需要从StringBuilder中获取String。

2.用空格字符分割字符串 - 使用 String.Split方法(Char []) [ ^ ]或 Regex.Split方法(字符串,字符串) [ ^ ](如属正则表达式你应该使用存储在静态只读字段中的编译正则表达式。

3.使用LINQ过滤和计算单词或在foreach块中手动计算单词。

1. You need to get String from that StringBuilder.
2. Split string by white-space characters - either use String.Split Method (Char[])[^] or Regex.Split Method (String, String)[^] (in case of Regex you should use compiled regex stored in static readonly field).
3. Filter and count words using LINQ or "manually" in foreach block.
var sb = new StringBuilder("hi friends hi");
var text = sb.ToString();

var words = Regex.Split(text, @"\W");
//var words = text.Split(' ', '\t', '\r', '\n');

var count = words.Where(i => i == "hi").Count();


这可能对你有所帮助:



http://stackoverflow.com/questions/9929279/to-count-the-frequency-of-each-word [ ^ ]



另外,如果您使用Google c#计算单词出现次数,您将获得许多其他可能的解决方案!



祝你好运!
This might be of help to you:

http://stackoverflow.com/questions/9929279/to-count-the-frequency-of-each-word[^]

Also, if you Google c# count word occurrence, you will get plenty of other possible solutions!

Good luck!


您好Mr.Subin



您可以执行以下操作:



Hi Mr.Subin

You could do the following:

using System.Text;

namespace SoundTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0;
            StringBuilder sb = new StringBuilder();
            sb.Append("hi user hi");
            string str = sb.ToString();
            string[] p = str.Split(' ');

            foreach (var item in p)
            {
                if (item == "hi") count++;
            }
            System.Console.WriteLine("count of hi = {0}",count);
        }
    }
}


这篇关于计算字符串生成器中的唯一字数(不是字母......单词))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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