VB Data Formatting & Conversion

String
Len(string): This function returns the length of string, and the number of characters it contains.

Left(string, number): It returns the number of characters equivalent to the specified number (in the function) beginning from the left end of string.

Right(string, number): It returns the number of characters specified by number from the right end of string.

Mid(string, position, number): It returns the number of characters specified by number starting at character number position from the left end of string.

InStr(string1, string2): This function returns the position of string2 in string1. It returns 0 if string2 is not found in string1.

LTrim(string), RTrim(string) and Trim(string): It returns string with non-significant spaces removed from the left, the right or both, respectively.

LCase(string), UCase(string): It returns a string converted to lower-case or upper-case, respectively.

Converting numeric data to string: To convert numeric data to string, you use the cstr function. Its purpose is to convert a numeric data to a numeric string:

Syntax: cstr(value) or stringvariable = cstr(value) Example: public sub converter Dim x As Integer, y As String x = 54678 y = cstr(x) 'contents of variable x have been converted to a numeric string and stored in y  print "I have concatenated x and y to: ",x&y end sub Word Count: Example 1: 'This example counts number of words in a text box Private Function WordCount(myCount As String) As Integer Dim theWords As String countme = Replace(myCount, " ", " ") theWwords = Split(myCount, " ") WordCount = UBound(theWords) + 1 End Function Private Sub txtTextBox_Change lblMyLabel.Caption = WordCount(txtTextBox.Text) & " words found." End Sub Example 2: Private Sub cmdCounting_Click 'This example also counts the number of words in Text Box Dim myCount, x, j As Integer, boolCounted As Boolean myCount = 1 txtMyTextBox.Text = Trim(TextBox1.Text) For x = 1 To Len(txtMyTextBox.Text) boolCounted = False If Mid(txtMyTextBox.Text, x, 1) = " " Then For j = i To Len(txtMyTextBox.Text) If Mid(txtMyTextBox.Text, j, 1) <> " " Then myCount = myCount + 1 i = j                boolCounted = True GoTo count_x End If         Next count_x: If boolCounted = False Then myCount= myCount + 1 End If     End If   Next MsgBox "The Text Box contains " & intcounter & " words", vbInformation End Sub

Joining Strings
The process of joining strings is known as concatenation. Visual basic uses two concatenation characters, + (adapted from earlier versions of VB) and & (used in the later versions).

Syntax: string [concatenator] string Example 1: strGreetings = "Hallo to you" + "my friend" strMyProp = "I have acquired" & "a new pet"

Example 2: Private Sub myExample Dim myNum, myVar1, myVar2 myNum= 2 + 2 ' Returns 4. myNum= 4257.04 + 98112 ' Returns 102369.04. myVar1 = "34": myVar2 = 6 ' Initialiing the mixed variables. myNum= myVar1 + myVar2 ' Result is 40. myVar1 = "34": myVar2 = "6" ' Initialize variables by assigning them strings. myNum = myVar1 + myVar2 ' Returns "346" (this is string concatenation). End Sub Separating string data and storing the separate strings into array elements

Joining string data extracted from different array elements into one string

Numeric
Converting numeric string data to numeric: A numeric string is a number appearing within quotes e.g "10", or a number stored in a string variable.To convert a numeric string to a numeric value, then we use the cint function. When you convert a numeric string to a number. The number can now then be used in arithmetic expressions.

Syntax: CInt(numericstring) or numericvariable = CInt(numericstring) Example: Private sub cmdNumbers_Click Dim n As String, k As Integer n = "6783" k = CInt(n) 'Converting n to Number and storing result in k   print "Using k in expression: ", k^2+5 End Sub Formatting an integer to scientific notation: print (Format(426, "#e+#")) ' Displays "42e+6". MsgBox(Format(132, "#e+0")) ' Displays "1e+3". Truncating a number: Print (Format(CSng("3.254"), "#.#")) ' Displays "3.2".

Numbers with embedded characters
Formatting Example: Private Sub cmdFormattingExample_Click Dim strMyInput As String Dim dblSampleNumber As Double strMyInput = InputBox("Please enter a number to demonstrate named numeric formats:") dblSampleNumber = Val(strMyInput) Print "Using '00000.00':"; Tab(25); Format$(dblSampleNumber, "00000.00") Print "Using '#####.##':"; Tab(25); Format$(dblSampleNumber, "#####.##") Print "Using '##,##0.00':"; Tab(25); Format$(dblSampleNumber, "##,##0.00") Print "Using '$##0.00':"; Tab(25); Format$(dblSampleNumber, "$##0.00") Print "Using '0.00%':"; Tab(25); Format$(dblSampleNumber, "0.00%") End Sub

Dates
Date:"ddddd" or "dddddd" are date format specifiers. Day of Week: The specifier for DayOfWeek ("w") and WeekOfYear ("ww") display the day that is considered as the first day of the week, and the week considered to be the first week of the year. Month: The specifier "m" character displays the month as a number without a leading zero, while the Quarter specifier ("q") displays the quarter of the year as a numeric value from 1 - 4.

Minutes: To display a minute as a number either with leading zeroes or without leading zeros, then you can use either the "N" or the "Nn" format specifier. Hours: The specifiers "Hh" are used to display the hour as a number with leading zeros, while specifying "ttttt" displays the time in full. AM or PM: To display either any hour as either morning or after noon, then use either "AM/PM", "am/pm", "A/P", "a/p", or "AMPM."

The specifier ("c") can be used to display a date and time in the "ddddd ttttt" format.

Year(Date) or Year(Now): Extracts the year from a date. Day(Date) : Extracts the day from a date e.g if Date = #3/23/2012#, then Day(Date) = 23. Month(Date):Extracts the month from a date. IsDate: It checks for a valid date. Can be used in validation to check (verify) if a given entry is of a valid date. CDate: It is used to convert a value to date e.g myDate = CDate(txtEntry.text), converts the contents of a control named txtEntry to a date.