1

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.

5
  • 1
    In your text theres only one "Lorem" if you take the case into account. Commented Oct 22, 2012 at 21:04
  • 3
    What about having "Loremo" instead of "Lorem", does it count? Commented Oct 22, 2012 at 21:05
  • no just Lorem and Loremo counts too Commented Oct 22, 2012 at 21:06
  • 1
    just "Lorem" or both "Lorem" and "Loremo"? Commented Oct 22, 2012 at 21:07
  • 1
    Maybe this question can be merged with this: stackoverflow.com/questions/3016522/… Commented Oct 22, 2012 at 21:11

6 Answers 6

9
string str = "Lorem ipsum is great. lorem ipsum Lorem...";
string word = "Lorem";
Console.WriteLine(Regex.Matches(str,word).Count);
Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

What if 'str = "Lorem ipsum Lorem. Lorem l"? You'd get 2, not three.
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).
@NikoDrašković: Edited the answer to take that into account.
I still don't think its complete: ideone.com/EXpBRr . Also, sorry about the nitpicking. Too bad OP isn't here to clarify what he actually wants.
@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.
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++;

Comments

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.

Comments

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"));

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.