String Manipulation

Count number of digits in string

The Char structure type provides a method called IsDigit(char c) to see whether a character in a string is a digit. This method takes only one argument. The argument is the character that you want to check. In the example below, we use IsDigit(char c) method to count the number of digits in a string "ID123223";

Example:

Module Module1

Sub Main()
  Dim st As String = "ID123223"
  Dim count As Integer = 0
  Dim i As Integer
  Dim arr() As Char = st.ToCharArray()
  For i = 0 To arr.Length - 1
    If Char.IsDigit(arr(i)) Then
         count = count + 1

End If
Next

Console.WriteLine("Number of digits:" & count)


End Sub

End Module

If you want to indicate whether a character in a specified position of a string is a digit, you can use IsDigit(string st, int position) method.

Example:

Module Module1

Sub Main()

  Dim name As String
  Console.Write("Enter your name:")
  name = Console.ReadLine()
  If Char.IsDigit(name, 0) Then
     Console.Write("The name can't be started with a digit.")
  Else : Console.WriteLine("Wecome " & name)
  End If
  Console.ReadLine()

End Sub

End Module



HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.