Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count how many specific characters in string

Tags:

I have a search box.

My admin user might search for "@MG @EB dorchester".

In ASP, I need to count how many times the symbol "@" appears in the string. How is the possible?

like image 284
TheCarver Avatar asked Sep 15 '11 00:09

TheCarver


People also ask

How do you count specific occurrences of characters in a string?

There are many ways for counting the number of occurrences of a char in a String. Let's start with a simple/naive approach: String someString = "elephant"; char someChar = 'e'; int count = 0; for (int i = 0; i < someString. length(); i++) { if (someString.

How do you count a specific character in a string in Excel?

To use the function, enter =LEN(cell) in the formula bar and press Enter. In these examples, cell is the cell you want to count, such as B1. To count the characters in more than one cell, enter the formula, and then copy and paste the formula to other cells.

How do you count the number of occurrences of an element in a string?

Python String count() The count() method returns the number of occurrences of a substring in the given string.

How do you count a specific character in a column in Excel?

Tip: If you want to count the total number of a specific character in a range, you can use this formula =SUMPRODUCT(LEN(A2:A6)-LEN(SUBSTITUTE(A2:A6,B2,""))), and A2:A2 is the range you want to count the specified character from, and B2 is the character you want to count.


2 Answers

Try this:

len(yourString) - len(replace(yourString, "@", "")) 
like image 200
Andrew Hare Avatar answered Sep 29 '22 01:09

Andrew Hare


Response.write ubound(split(str,"@")) 

is enough for counting the occurance of a specific character

like image 44
srawn Avatar answered Sep 28 '22 23:09

srawn