Write special characters in a string

What is the easiest way to count the number of occurrences of a particular character in a string?

i.e. I need to write the countTheCharacters () function to

str="the little red hen" count=countTheCharacters(str,"e") 'count should equal 4 count=countTheCharacters(str,"t") 'count should equal 3 
+54
string
Mar 04 2018-11-12T00:
source share
26 answers

The simplest thing is to simply skip the characters in the line:

 Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer Dim cnt As Integer = 0 For Each c As Char In value If c = ch Then cnt += 1 End If Next Return cnt End Function 

Using:

 count = CountCharacter(str, "e"C) 

Another approach, which is almost as efficient and provides shorter code, is to use LINQ extension methods:

 Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer Return value.Count(Function(c As Char) c = ch) End Function 
+62
Mar 04 2018-11-12T00:
source share

This is an easy way.

 text="the little red hen" count = text.Split("e").Length -1 ' Equals 4 count = text.Split("t").Length -1 ' Equals 3 
+56
Jul 06 2018-12-12T00:
source share

You can try this

 Dim occurCount As Integer = Len(testStr) - Len(testStr.Replace(testCharStr, "")) 
+28
Nov 11 '11 at 14:55
source share

Here is a simple version.

 text.count(function(x) x = "a") 

The above will give you the number a in a string. If you want to ignore the case:

 text.count(function(x) Ucase(x) = "A") 

Or if you just want to count the letters:

 text.count(function(x) Char.IsLetter(x) = True) 

Take a picture!

+10
Jul 21 '14 at 21:36
source share

Or (in VB.NET):

 Function InstanceCount(ByVal StringToSearch As String, ByVal StringToFind As String) As Long If Len(StringToFind) Then InstanceCount = UBound(Split(StringToSearch, StringToFind)) End If End Function 
+4
May 20 '11 at 19:30
source share

Convert Ujjwal Manandhar code to VB.NET as follows:

 Dim a As String = "this is test" Dim pattern As String = "t" Dim ex As New System.Text.RegularExpressions.Regex(pattern) Dim m As System.Text.RegularExpressions.MatchCollection m = ex.Matches(a) MsgBox(m.Count.ToString()) 
+3
Oct 19 2018-11-21T00:
source share

Thanks, @guffa . The ability to do this on a single line or even in a longer statement in .NET is very convenient. This VB.NET example counts the number of LineFeed characters:

 Dim j As Integer = MyString.Count(Function(c As Char) c = vbLf) 

j returns the number of LineFeeds in MyString.

+3
Feb 05 2018-02-17T00:
source share
 Public Function CountOccurrences(ByVal StToSerach As String, ByVal StToLookFor As String) As Int32 Dim iPos = -1 Dim iFound = 0 Do iPos = StToSerach.IndexOf(StToLookFor, iPos + 1) If iPos <> -1 Then iFound += 1 End If<br/> Loop Until iPos = -1 Return iFound End Function 

Code Usage:

 Dim iCountTimes As Integer = CountOccurrences("Can I call you now?", "a") 

You can also use it as an extension:

 <Extension()> _ Public Function CountOccurrences(ByVal StToSerach As String, ByVal StToLookFor As String) As Int32 Dim iPos = -1 Dim iFound = 0 Do iPos = StToSerach.IndexOf(StToLookFor, iPos + 1) If iPos <> -1 Then iFound += 1 End If Loop Until iPos = -1 Return iFound End Function 

Code Usage:

 Dim iCountTimes2 As Integer = "Can I call you now?".CountOccurrences("a") 
+2
Jun 16 2018-12-12T00:
source share
 Public Class VOWELS Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim str1, s, c As String Dim i, l As Integer str1 = TextBox1.Text l = Len(str1) c = 0 i = 0 Dim intloopIndex As Integer For intloopIndex = 1 To l s = Mid(str1, intloopIndex, 1) If (s = "A" Or s = "a" Or s = "E" Or s = "e" Or s = "I" Or s = "i" Or s = "O" Or s = "o" Or s = "U" Or s = "u") Then c = c + 1 End If Next MsgBox("No of Vowels: " + c.ToString) End Sub End Class 
+2
09 Oct '12 at 16:10
source share

When I found this solution, I was looking for something a little different, since the line I wanted to count was longer than one character, so I came up with this solution:

  Public Shared Function StrCounter(str As String, CountStr As String) As Integer Dim Ctr As Integer = 0 Dim Ptr As Integer = 1 While InStr(Ptr, str, CountStr) > 0 Ptr = InStr(Ptr, str, CountStr) + Len(CountStr) Ctr += 1 End While Return Ctr End Function 
+2
Oct 09
source share

I think this would be the easiest:

 Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer Return len(value) - len(replace(value, ch, "")) End Function 
+2
May 15 '14 at 19:45
source share

Another possibility is to work with Split:

 Dim tmp() As String tmp = Split(Expression, Delimiter) Dim count As Integer = tmp.Length - 1 
+1
Mar 04 2018-11-12T00:
source share

I suggest you do like this:

 String.Replace("e", "").Count String.Replace("t", "").Count 

You can also use .Split("e").Count - 1 or .Split("t").Count - 1 respetivelly, but it gives incorrect values โ€‹โ€‹if, for example, you have e or t at the beginning of String

+1
Jan 05 '13 at 21:32
source share

Using regular expressions ...

 Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer Return (New System.Text.RegularExpressions.Regex(ch)).Matches(value).Count End Function 
+1
Aug 19 '13 at 15:53 โ€‹โ€‹on
source share
 eCount = str.Length - Replace(str, "e", "").Length tCount = str.Length - Replace(str, "t", "").Length 
+1
Jun 25 '15 at 1:39 on
source share

Another possibility is to use a regular expression:

 string a = "this is test"; string pattern = "t"; System.Text.RegularExpressions.Regex ex = new System.Text.RegularExpressions.Regex(pattern); System.Text.RegularExpressions.MatchCollection m = ex.Matches(a); MessageBox.Show(m.Count.ToString()); 

Please convert this to VB.NET.

0
Mar 04 '11 at 15:59
source share

I found the best answer: P:

 String.ToString.Count - String.ToString.Replace("e", "").Count String.ToString.Count - String.ToString.Replace("t", "").Count 
0
Jan 07 '13 at
source share
  'trying to find the amount of "." in the text 'if txtName looks like "hi...hi" then intdots will = 3 Dim test As String = txtName.Text Dim intdots As Integer = 0 For i = 1 To test.Length Dim inta As Integer = 0 + 1 Dim stra As String = test.Substring(inta) If stra = "." Then intdots = intdots + 1 End If Next txttest.text = intdots 
0
08 Oct '13 at 2:36
source share

I am using LINQ, and the solution is very simple:

Code in C #:

 count = yourString.ToCharArray().Count(c => c == 'e'); 

Code in function:

 public static int countTheCharacters(string str, char charToCount){ return str.ToCharArray().Count(c => c == charToCount); } 

Call function:

 count = countTheCharacters(yourString, 'e'); 
0
Jan 23 '14 at 16:08
source share

Using:

 Function fNbrStrInStr(strin As Variant, strToCount As String) fNbrStrInStr = UBound(Split(strin, strToCount)) - LBound(Split(strin, strToCount)) End Function 

I used strin as an option to handle very long text. Separation can be based on zero or on the basis of one for the low end, depending on user settings, and subtraction guarantees the correct amount.

I did not include checking that strcount is longer than strin to make the code concise.

0
Feb 20 '15 at 5:25
source share

What are the huge codes for something so simple:

In C #, create an extension method and use LINQ.

 public static int CountOccurences(this string s, char c) { return s.Count(t => t == c); } 

Using:

 int count = "toto is the best".CountOccurences('t'); 

Result: 4.

0
Sep 02 '15 at 7:31
source share

var charCount = "string with periods...".Count(x => '.' == x);

0
Oct 22 '15 at 14:19
source share

I am using the following function. This is not the most efficient memory, but it is very easy to understand, supports several comparison methods, only 4 lines, quickly, mainly works in VBA, finds not only individual characters, but any search string (I often look for VbCrLf (s)).

The only thing missing is the ability to start the search from another "Start"

  Function inStC(myInput As String, Search As String, Optional myCompareMethod As Long = CompareMethod.Text) As Long If InStr(1, myInput, Search, myCompareMethod) = 0 Then Return 0 Return UBound(Split(myInput, Search,, myCompareMethod)) End Function 

One thing that I like is a compact use case.

 str="the little red hen" count=inStC(str,"e") 'count should equal 4 count=inStC(str,"t") 'count should equal 3 

While I'm here, I would like to sew up my inStB function, which, instead of returning the number of rows, will simply return a boolean if a search string is present. I need this feature often, and it makes my code cleaner.

 Function inStB(myInput As String, Search As String, Optional Start As Long = 1, Optional myCompareMethod As Long = CompareMethod.Text) As Boolean If InStr(Start, myInput, Search, myCompareMethod) > 0 Then Return True Return False End Function 
0
Feb 12 '17 at 13:03 on
source share

Using:

 Dim a inputString = InputBox("Enter String", "Enter Value", "") MyString = UCase(inputString) MsgBox MyString Dim stringLength stringLength = Len(MyString) Dim temp output = "" i = 1 Do temp = Mid(MyString, i, 1) MsgBox temp & i CharacterCount = len(MyString) - len(Replace(MyString, temp, "")) MyString = Replace(MyString, temp, "") output = output & temp & ": " & CharacterCount & vbNewline Loop While MyString <> "" MsgBox output 
-2
Feb 26 '14 at 11:34
source share
 Private Sub Data_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Data.KeyPress If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) And Not e.KeyChar = "." Then e.Handled = True Else If e.KeyChar = "." And Data.Text.ToCharArray().Count(Function(c) c = ".") > 0 Then e.Handled = True End If End If End Sub 
-3
Jul 23 '14 at 10:56 on
source share

Here is the direct code that solves the OP problem:

  Dim str As String = "the little red hen" Dim total As Int32 Dim Target As String = "e" Dim Temp As Int32 Dim Temp2 As Int32 = -1 Line50: Temp = str.IndexOf(Target, Temp2 + 1) Temp2 = Temp If Temp <> -1 Then ' Means there is a target there total = total + 1 GoTo Line50 End If MessageBox.Show(CStr(total)) 

Now this is a handy feature to solve the OP problem:

  Public Function CountOccurrence(ByVal YourStringToCountOccurrence As String, ByVal TargetSingleCharacterToCount As String) As Int32 Dim total As Int32 Dim Temp As Int32 Dim Temp2 As Int32 = -1 Line50: Temp = YourStringToCountOccurrence.IndexOf(TargetSingleCharacterToCount, Temp2 + 1) Temp2 = Temp If Temp <> -1 Then ' Means there is a target there total = total + 1 GoTo Line50 Else Return total End If End Function 

Function usage example:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim str As String = "the little red hen" MessageBox.Show(CStr(CountOccurrence(str, "e"))) ' It will return 4 End Sub 
-6
Jun 26 2018-11-22T00:
source share



All Articles