Count the unique number of words inside a string builder(Not letters...Words))
-
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
-
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
Probably not very efficient: Get the string by calling ToString(). Split by space to get an arry of words. You can then use Distinct to get unique words and/or Select to get a specific word
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
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
Something like this?
StringBuilder sb = new StringBuilder();
sb.Append("hi friends hi");Console.WriteLine("Count is " + "".ToString().Split(new []{" "}, StringSplitOptions.RemoveEmptyEntries).
Where(w => w.Equals("hi", StringComparison.CurrentCultureIgnoreCase)).Count();Andreas Johansson
Senior software developer at Tieto Sweden -
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
Look at http://www.dotnetperls.com/word-count[^]
public static int CountWords1(string s)
{
MatchCollection collection = Regex.Matches(s, @"[\S]+");
return collection.Count;
} -
Look at http://www.dotnetperls.com/word-count[^]
public static int CountWords1(string s)
{
MatchCollection collection = Regex.Matches(s, @"[\S]+");
return collection.Count;
}Yeah that's how I would do it, but copying your homework from Google is frowned upon. I think the OP should work to understand what the problem is, a method for solving it, and then the code for that method. Skipping to the last part won't help you pass the class.
-
Yeah that's how I would do it, but copying your homework from Google is frowned upon. I think the OP should work to understand what the problem is, a method for solving it, and then the code for that method. Skipping to the last part won't help you pass the class.
Jasmine2501 wrote:
Skipping to the last part won't help you pass the class.
That's why more and more companies are giving a coding-test when applying for a job. If the OP wants to cheat, he'll find that he has been cheated soon enough, owning a degree and not being able to use it :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Jasmine2501 wrote:
Skipping to the last part won't help you pass the class.
That's why more and more companies are giving a coding-test when applying for a job. If the OP wants to cheat, he'll find that he has been cheated soon enough, owning a degree and not being able to use it :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
These days, I'm almost insulted when I haven't been asked to do a coding test. And, I did have a job recently where the interview should have clued me in to the fact they didn't know what the hell was going on... worked there for two months - everybody was real smart and legit with their skills - but I only did one productive thing the whole time. I should have known something was hinky in the interview when they did zero actual assessment of my skills.
-
These days, I'm almost insulted when I haven't been asked to do a coding test. And, I did have a job recently where the interview should have clued me in to the fact they didn't know what the hell was going on... worked there for two months - everybody was real smart and legit with their skills - but I only did one productive thing the whole time. I should have known something was hinky in the interview when they did zero actual assessment of my skills.
-
Oh heck yeah, I had another gig lined up before I quit from there. It's hard to stay unemployed if you have programming skills and you're being honest about it :)
-
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
I'd use a Hashset.
-
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
Something like:
sb.ToString().Split(' ').ToList().Distinct().Count()
That does (or should be close to) what your subject line wants to do. But that's different from what you wrote:
kanamala subin wrote:
i want to check the count of "hi" in the sb
so my final output would be like this.
Count is 2.Which is "count the number of instances of a word", for which you could do something like this:
Dictionary wordCount = new Dictionary();
sb.ToString().Split(' ').ToList().ForEach(w=>
{
if (wordCount.ContainsKey(w) ++wordCountCount[w];
else wordCount[w]=1;
}Marc
Testers Wanted!
Latest Article: User Authentication on Ruby on Rails - the definitive how to
My Blog