0

I have a string in .NET like so:

string str = "Lorem ipsum is great. lorem ipsum Lorem...";

I need to get a count of all "Lorem" that matches case as well. So Lorem should appear twice and ignore the lorem.

Thanks.

Francis P
  • 13,377
  • 3
  • 27
  • 51
cdub
  • 24,555
  • 57
  • 174
  • 303

6 Answers6

8
string str = "Lorem ipsum is great. lorem ipsum Lorem...";
string word = "Lorem";
Console.WriteLine(Regex.Matches(str,word).Count);
Francis P
  • 13,377
  • 3
  • 27
  • 51
skjcyber
  • 5,759
  • 12
  • 40
  • 60
6

You can use Linq.

String searchWhat = "Lorem";
int count = str.Split(new[]{' ','.'}, StringSplitOptions.None)
               .Count(w => w == searchWhat);

demo: http://ideone.com/a9XHln

Edit: You have commented that "Lorem Loremo" would count as two, so you want to count all occurences of a given word(case-sentive) even if that word is part of another word. Then you could use String.Contains:

int count = str.Split(new[]{' ','.'}, StringSplitOptions.None)
               .Count(w => w.Contains(searchWhat));

demo: http://ideone.com/fxDGuf

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • What if 'str = "Lorem ipsum Lorem. Lorem l"? You'd get 2, not three. – neeKo Oct 22 '12 at 21:08
  • I admit, it's not true anymore. What about other punctuation? Question comments also indicate that Loremo would have to match (although the comment is a bit ambiguous). – neeKo Oct 22 '12 at 21:12
  • @NikoDrašković: Edited the answer to take that into account. – Tim Schmelter Oct 22 '12 at 21:17
  • I still don't think its complete: http://ideone.com/EXpBRr . Also, sorry about the nitpicking. Too bad OP isn't here to clarify what he actually wants. – neeKo Oct 22 '12 at 21:25
  • 1
    @NikoDrašković: Of course you can add a `Char[]` with all allowed delimiters between words. The accepted answer is better if OP doesn't want to count words but occurences(even multiple "Lorem" in a single word like "LoremoLorem"). But that's a different approach and requirement. – Tim Schmelter Oct 22 '12 at 21:37
0

Can be done using Linq:

string str = "Lorem ipsum is great. lorem ipsum Lorem...";
int loremCount = str.Split(new[]{' ','.',','}, StringSplitOptions.None).Count(s => s.Equals("Lorem"));

If you want to consider "Loremo":

int loremCount = str.Count(s => s.Equals("Lorem"));
Francis P
  • 13,377
  • 3
  • 27
  • 51
0

Here's my 2 cents. It will find all instances of "Lorem" case sensative, but it will return a count for things that have "Lorem" in it, like "Loremo" or "thismightnotLorembewhatyouwant".

The question was a bit vague so this answer is a quick solution that conforms to what you requested.

string test = "Lorem ipsum is great. lorem ipsum Lorem...";

int pos = -1;
int count = 0;
while ((pos = test.IndexOf("Lorem", pos+1)) != -1)
    count++;
vane
  • 2,125
  • 1
  • 21
  • 40
0

If you want this to be able to perform other operations, you can dump the entire string into a List and then you could run Linq queries off off that list.

var phrase = "Lorem ipsum...";
var wordList = phrase.Split(' ').ToList();
var loremCount = wordList.Where(x => x.ToLower() == "lorem").Count();

This way wordList is reusable.

Christopher Rayl
  • 319
  • 2
  • 10
0

Use the below code:

using System.Text.RegularExpressions;

string text = "Lorem ipsum is great. lorem ipsum Lorem...";
int count = new Regex("Lorem").Matches(text).Count;

Hope it will help you. If not please let me know.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Hasinur
  • 194
  • 1
  • 6