1
string containsCharacter = textBox1.Text;
string testString = "test string contains certain characters";
int count = testString.Split(containsCharacter).Length - 1;

I originally pulled this code off another person's question's answer but it doesn't seem to work with text boxes.

Errors I'm getting:

The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
Argument 1: cannot convert from 'string' to 'char[]'

I prefer to fix this code rather than use other things like LINQ but I would accept it if there isn't a way to fix this code.

6
  • @JeroenVannevel Occurances of a certain character in that string. Commented Nov 30, 2013 at 17:56
  • 1
    @puretppc - You should pass char as parameter than instead of string. Commented Nov 30, 2013 at 17:56
  • @RohitVats Yup it finally works. Commented Nov 30, 2013 at 17:59
  • Great. Sometimes errors are self explanatory as well. Listening to them can help you solving the problem as well. :) Commented Nov 30, 2013 at 18:02
  • 1
    Use other overload then which accepts string[] - int count = testString.Split(new string[] { containsCharacter }, StringSplitOptions.None).Length - 1; Commented Nov 30, 2013 at 18:23

6 Answers 6

5

You could iterate through the characters

        string value = "BANANA";
        int x = 0;
        foreach (char c in value)
        {
            if (c == 'A')
                x++;
        }
Sign up to request clarification or add additional context in comments.

5 Comments

In the age of LINQ manual counting is obsolete except in narrow scenarios or when performance is critical.
Your code reminds me the way I coded in c#2.0, Later I just prefer var count = s.Count(x=> x== 'A');
@SriramSakthivel I am one of the biggest fans of old code ever; but Split is also old code (and this answer, as it is now, can only deal with single characters, what makes it a bit restrictive) :)
@varocarbas Yup, I agree with you. on the other hand split is not a old school code even today for splitting you'll have to use split only I don't see any alternate. :)
@puretppc to count string matches like "AN" i would use what varocarbas recommends in his answer. value.Split(new string[] { "AN" }, StringSplitOptions.None).Count()-1
3
string containsCharacter = "t";
string testString = "test string contains certain characters";
int count = testString.Count(x => x.ToString() == containsCharacter);

This example will return 6.

4 Comments

And what about if you want to know the number of times that "test" appears in testString?
Your code looks for characters (this is what X is) and, unless you can find a character being "test", I am afraid that it will not work.
Then I would use a regular expression instead. Something like Regex.Matches(testString , "test").Count and properly escape the test-for string.
Sure, there are many alternatives. But the original code of the OP was a pretty good one (Split), although restricted to characters, so why not adapting it to strings (or convert the input string into characters to avoid the error)? But you can propose any solution, include a regex-based complement, if you think that this is better.
2

The Split version you are using expects a character as input. This is the version for strings:

    string containsText = textBox1.Text;
    string testString = "test string contains certain characters";
    int count = testString.Split(new string[]{containsText}, StringSplitOptions.None).Length - 1;

With this code, count will be: 1 if textBox1.Text includes "test", 6 if it contains "t", etc. That is, it can deal with any string (whose length might be one, as a single character, or as big as required).

Comments

1

You can call ToCharArray on the string to make it a char[], like this:

int count = testString.Split(containsCharacter.ToCharArray()).Length - 1;

Since Split takes characters as a param, you could rewrite this by listing the characters being counted directly, as follows:

int count = testString.Split(',', ';', '-').Length - 1;

Comments

1
"this string. contains. 3. dots".Split(new[] {"."}, StringSplitOptions.None).Count() - 1

Comments

1

Edit: Upon Reading your code more carefully I suggest you do this, you should rephrase your question to "Check the number of occurences of a certain string in Another string":

string containsString = "this";
string test = "thisisateststringthisisateststring";
var matches = Regex.Matches(test,containsString).Count;

matches is 2!

My initial post answers your actual question "occurrences of a certain character in a string":

string test = "thisisateststring";
int count = test.Count(w => w == 'i');

Count is 3!

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.