Count the number of occurrences of a character in a string
str.count(sub[, start[, end]])
Return the number of non-overlapping occurrences of substring
sub
in the range[start, end]
. Optional argumentsstart
andend
are interpreted as in slice notation.
>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4
Finding the count of characters and numbers in a string
If I understand correctly you are using Oracle PLSQL, and as far as I know, there isn't any "built-in" method (in PLSQL) that counts the number of digits/characters in a string.
But, you can do the following to count characters:select LENGTH(REGEXP_REPLACE('abcd12345','[0-9]')) from dual
and digits:select LENGTH(REGEXP_REPLACE('abcd12345','[a-zA-Z]')) from dual
Or, in your case:
select name,
LENGTH(REGEXP_REPLACE(name,'[a-zA-Z]','')) as num_count,
LENGTH(REGEXP_REPLACE(name,'[0-9]','')) as char_count,
from test6;
For Bill the Lizard:
My answer was tested on Oracle 11g and it works just fine!
If you decide to delete my answer again, please be kind enough to add a comment that explains why. I was also looking for you in the chat rooms...
Count the number of occurrences of a character in a string in Javascript
I have updated this answer. I like the idea of using a match better, but it is slower:
console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3
console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4
How can you count the characters in a string with for loop
You should be incrementing numbers
or letters
when x.isnumeric()
or x.isalpha()
respectively. And you should be printing out numbers
and letters
instead of x[letters]
, which will more than likely throw an Exception since x
is a string of length 1 and letters
may very well be greater than 0.
string = input("Enter something ")
numbers = 0
letters = 0
for x in string:
if x.isnumeric():
numbers += 1
elif x.isalpha():
letters += 1
print('String:', string)
print("Letters:", letters)
print("Numbers:", numbers)
How would you count occurrences of a string (actually a char) within a string?
If you're using .NET 3.5 you can do this in a one-liner with LINQ:
int count = source.Count(f => f == '/');
If you don't want to use LINQ you can do it with:
int count = source.Split('/').Length - 1;
You might be surprised to learn that your original technique seems to be about 30% faster than either of these! I've just done a quick benchmark with "/once/upon/a/time/" and the results are as follows:
Your original = 12s
source.Count = 19s
source.Split = 17s
foreach (from bobwienholt's answer) = 10s
(The times are for 50,000,000 iterations so you're unlikely to notice much difference in the real world.)
How do I count the number of occurrences of a char in a String?
My 'idiomatic one-liner' for this is:
int count = StringUtils.countMatches("a.b.c.d", ".");
Why write it yourself when it's already in commons lang?
Spring Framework's oneliner for this is:
int occurance = StringUtils.countOccurrencesOf("a.b.c.d", ".");
Count the number of Strings in a String
if (!text.contains(symbol)) {
break;
}
count++;
if ((text.indexOf(symbol, 1)) == -1) {
break;
}
This part of your code is quite problematic. You increment the count if the symbol is present in the text, even if it is the first character - while your
text = text.substring(text.indexOf(symbol, 1));
code assures that the first character is the specified symbol in the text. This causes your code to overcount by one if it contains the specified symbol. To fix it, change your code to
while (true) {
if (!text.contains(symbol)) {
break;
}
count++;
text = text.substring(text.indexOf(symbol)+1);//+1 so it starts at the next character
System.out.println(text);
System.out.println(count);
}
As you can see, the first character is no longer the specified symbol. This ensures that
- The
contains()
method is enough to determine the presence of the symbol - The
count
is set correctly
Related Topics
What Is the SQL for 'Next' and 'Previous' in a Table
Changing SQL Server Database Sorting
Casting Scientific Notation (From Varchar -> Numeric) in a View
How to Specify in Clause in a Dynamic Query Using a Variable
Postgresql Does Not Use a Partial Index
Bigquery Group_Concat and Order By
How to Use Like and in for a Where Statment
Ms-Access -> Select as + Order by = Error
Generate Nested Nth Level JSON SQL Server Using Recursive Cte
How to List Active Connections on Postgresql
Selecting Specific Row Number in SQL
SQL Server After Update Trigger
SQL Server 2005 Recursive Query with Loops in Data - Is It Possible
Exporting SQL Server Table to Multiple Part Files