ASP.Net 101

A collection of Interesting Articles about ASP.net

How to count # of character occurrences in a string .NET vERSION

 A quick way to find the number of occurrences of a character in a string could go something as shown below.  

Dim x As String“aa:bb:cc:dd”

 Dim num As Integer = Len(x) – Len(Replace(x, “:”, “”))

February 5, 2009 Posted by | T-SQL in SQL Server 2005 | Leave a Comment

How to count # of character occurrences in a string

 A quick and dirty way would be to use an expression like

length(x) – length(replace(x, “:”, “”))

Here’s some code that I actually ran:

set @x = “aa:bb:cc:dd”;
set @colons = length(@x) – length(replace(@x, “:”, “”));
select @x ,  @colons;

 

Example 2:

try to count how many times p appears in word ‘Mississippi’

select len(‘Mississippi’) – len(replace(‘Mississippi’, ‘p’, ”))

February 5, 2009 Posted by | T-SQL in SQL Server 2005 | Leave a Comment

   

Follow

Get every new post delivered to your Inbox.