4

Given a String array such as this:

string[]={"bmw"," ","1bmw"," "};

I need to count how often the substring bmw occurs in that array. In this example it occurs 2 times.

How do I write that in C#?

also i want to ignore the capital character,

sting[]={"bmw", "", "BMw","1bmw"}

then the count result is 3.

what should i do?

#

Thanks for everyone's answer.

6 Answers 6

15

Try:

var str = new string[] { "bmw", " ", "1bmw", " " };
var count = str.Count(s => s.Contains("bmw"));

the next code will return the count of case insensitive occurrences (as author mentioned in comments):

var count = str.Count(s =>
    s.IndexOf("bmw", StringComparison.OrdinalIgnoreCase) > -1
);

More about string comparisons could be found here.

Sign up to request clarification or add additional context in comments.

1 Comment

hi, how about the sting[]={"bmw", "", "BMw","1bmw"} there is a "BMw" not exact same with "bmw" i also want to count this word in.
5

Without LINQ

int counter = 0;

foreach(string item in string_array)
{
   if(item.IndexOf("bmw") > -1){
     counter++;

   }
}

2 Comments

hi, how about the sting[]={"bmw", "", "BMw","1bmw"} there is a "BMw" not exact same with "bmw" i also want to count this word in.
@shaon-fan The easiest way to handle that would be to use ToLower() on each of the strings to search.
3

You can use Linq for this like so:

var s = new string[] {"bmw", "1bmw"};
var result = s.Where(o=>o.Contains("bmw")).Count();

1 Comment

hi, how about the sting[]={"bmw", "", "BMw","1bmw"} there is a "BMw" not exact same with "bmw" i also want to count this word in.
1

If you're using C# 3, you can use LINQ:

var strings = {"bmw"," ","1bmw"," "};
var count = string.Where( s => s.IndexOf( "bmw" ) > -1 ).Count();

Otherwise a loop will work too:

string[] strings = {"bmw"," ","1bmw"," "};  
int count = 0;
foreach( string s in strings ) {
    if( s.IndexOf( "bmw" > -1 ) ) {
       count++;
    }
}

2 Comments

hi, how about the sting[]={"bmw", "", "BMw","1bmw"} there is a "BMw" not exact same with "bmw" i also want to count this word in.
@shaon-fan: If you want case-insensitive comparison, just use: IndexOf( "bmw", StringComparison.OrdinalIgnoreCase ) - the OrdinalIgnoreCase parameter will match any casing in the comparison.
1

One method using LINQ

static int CountOfString(IEnumerable<string> items, string substring, bool isCaseSensitive)
{
    StringComparison comparison = StringComparison.InvariantCulture;
    if (!isCaseSensitive)
        comparison = StringComparison.InvariantCultureIgnoreCase;

    return items.Count(item => item.IndexOf(substring, comparison) > -1);
}

Using it

string[] items = { "bmw", " ", "1bmw", " " };
int count = CountOfString(items, "bmw", false);

Comments

1

Case-insensitiveness makes it a little more verbose but still it's quite compact using following:

var str = new sting[]={"bmw", "", "BMw","1bmw"};
var count = str.Count(s => s.IndexOf("bmw", StringComparison.InvariantCultureIgnoreCase) > -1);

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.