Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Count Number of Occurrences of a Character in String C#, VB.NET

Jul 25, 2016
Introduction

Here I will explain how to count number of occurrences of character in string in
asp.net using c#, vb.net with example or asp.net get number of occurrences of a character in a string or find number of characters occurrences in string in asp.net using c#, vb.net with example. We have different ways to get number of occurrences of character in string like loop through the string and get number of character occurrences or use LINQ queries to get character occurrences in string in c#, vb.net.


To count number of occurrences of character in string in asp.net using c#, vb.net first write the code following code in your aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Count Number of Occurrences of Character in string C#, VB.NET</title>
<style type="text/css">
.txtclass
{
width:250px;

}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter Name:</td>
<td><asp:TextBox ID="txtName" runat="server" Text="welcome to aspdotnet-suresh.com" CssClass="txtclass" /></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnCount" runat="server" Text="Count"
onclick="btnCount_Click" /></td>
</tr>
</table>
<asp:Label ID="lblResult" runat="server"/>
</div>
</form>
</body>
</html>

Now open code behind file and write following code

C# Code


protected void btnCount_Click(object sender, EventArgs e)
{
string str = txtName.Text;
string result = string.Empty;
while(str.Length>0)
{
int count = 0;
for (int j = 0; j < str.Length; j++)
{
if (str[0] == str[j])
{
count++;
}
}
result += str[0] + " : " + count + "<br>";
str = str.Replace(str[0].ToString(), string.Empty);
}
lblResult.Text = result;
}

VB.NET Code


Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnCount_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim str As String = txtName.Text
Dim result As String = String.Empty
While str.Length > 0
Dim count As Integer = 0
For j As Integer = 0 To str.Length - 1
If str(0) = str(j) Then
count += 1
End If
Next
result += str(0) + " : " + Convert.ToString(count) + "<br>"
str = str.Replace(str(0).ToString(), String.Empty)
End While
lblResult.Text = result
End Sub
End Class

Demo

Following is the demo to count number character occurrences in string.


Count Number of Occurrences of a Character in String C#, VB.NET
Note: Here i explained basic looping way to find number of character occurrences in string we have a lot of other ways to write same functionality in efficient manner. 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.