Wednesday, July 20, 2011

Strings, Math Functions

Strings
Strings in Visual Basic are supported by the .NET String class. The String data type can represent a series of characters and can contain approximately up to 2 billion Unicode characters. There are many built-in functions in the String class. Some .NET Framework functions are also built into the String class. The following code puts some String functions to work.

Imports System.Console
Module Module1

Sub Main()
Dim string1 As String = "Strings in Visual Basic"
Dim string2 As String
Dim string3 As String
Dim string4 As String
Dim string5 As String
Dim string6 As String
string2 = UCase(string1)
'converts string to uppercase
string3 = LCase(string1)
'converts string to lowercase
string4 = string1.Substring(11, 6)
'returns a substring
string5 = string1.Clone
'clones a string
string6 = string1.GetHashCode
'gets the hashcode
WriteLine(string2)
WriteLine(string3)
WriteLine(string4)
WriteLine(string5)
WriteLine(string6)
Read()
End Sub

End Module

The image below displays output from above code.


Math Functions
Visual Basic provides support for handling Mathematical calculations. Math functions are stored in System.Math namespace. We need to import this namespace  when we work with Math functions. The functions built into Math class helps us calculate the Trigonometry values, Square roots,  logarithm values, etc. The following code puts some Math functions to work.

Imports System.Console
Imports System.Math
Module Module1

Sub Main()
WriteLine("Sine 90 is" & " " & Sin(90))
'display Sine90 value
WriteLine("Square root of 81 is " & " " & Sqrt(81))
'displays square root of 81
WriteLine("Log value of 12 is" & " " & Log(12))
'displays the logarithm value of 12
Read()
End Sub

End Module

The image below displays output from above code.

No comments:

Post a Comment