VB.Net - DataTypes - String

String

You assign string using the double quote delimiter. For example

1
dim s as string = "hello world"

Sometimes you need the character data type, you can simulate this by placing the lower case c at the end of a string assignment. For example

1
2
dim s as string = "(this,that,and the other)
dim this as string = s.Split(","c)(0)

That second assignment (creating the this variable) did that by calling the Split function of a string. This function requires a Character datatype so I suffixed “,” with the c to provide a character.

Converting String to Numbers

There are functions used to convert a string into a number, for example cdbl(stringvar) and cint(stringvar) which will convert a string into a double or integer.

? cdbl(9)
= 9

? cdbl(“9”)
= 9

? cdbl(“”)
throws error

Checking a String to see if it’s a number

1
2
3
4
5
6
Private Function IsValidNumber(input As String) As Boolean
Dim pattern As String = "^\d+(\.\d+)?$"
Dim regex As New Regex(pattern)

Return regex.IsMatch(input)
End Function