Wednesday, December 12, 2012

String and format function in visual basic


VB provides String functions such as Left, Right, and Len.
1.      Left(String Expression, no. of characters)
Ex. Left(txtName.text,5)              Returns first 5 characters
2.      Right(String Expression, no. of characters)
Ex. Right(strName,1)       Returns last one character
String expression in Left and Right statements may be a string variable, string literal or text properties. Number of characters and start position are both numeric and may be variables, literals or numeric expressions.
3.      Len(String Expression)
Ex. Len(“Visual Basic”)   Returns no. of character as 12
We can use the len function to determine the length of string expression. We may need to know how many characters the user has entered or how long list element is. The value returned by the len function is an integer count of the number of characters in the string.

Format Functions:
i. The FormatCurrency Function :
The FormatCurrency function returns a string of character formated as dollar and cents by default , The currency value display a dollar sign and comma and two postion to the right of the decimal point.
Syntax:
FormatCurrency ( NumericExpressionToFormat )
Example:
lblBalance.Caption = FormatCurrency ( Balance )
curBalance 1275.675 FormatCurrency ( curBalance ) $1,275.68
ii. The FormatNumber Function :
The FormatNumber function is similar to the FormatCurrency function. The default format is determine by your computer aegional setting. It will generally display commas and two digit to the right of the decimal point.
Syntax:
FormatNumber ( Expression-To-Format )
Example:
lblSum.Caption = FormatNumber ( Sum )
Varible Value Funtion Output
mcurTotal 1125.67 FormatNumber ( mcurTotal , 0 ) 1,126
curBalance 1234.567 FormatNumber ( curBalance , 2 ) 1,234.57
iii.The FormatPercent Function :
To display numeric values as a percent ,use the FormatPercent( ) . This function multiplies the argument by 100 , adds a percent sign, and rounds to the decimal places.
Syntax :
FormatPercent ( ExpressionToFormat [ , NamedFormat ] )
Example :
lblInterest.Caption = FormatPercent ( Rate )
In the complete form of the FormatPercent function. You can select the number of digit to the right of the decimal point .
Varible Value Function Output
curCorrect .75 FormatPercent ( curCorrect ) 75%
curCorrect .75 FormatPercent ( curCorrect , 1 ) 75.0%
iv. The FormatDateTime Function :
You can format an expression as a date or a time . The Expression may be a string that holds a date or time value , a date type varible or a function that return a date.
If you omit the optional named format, the function returns the date using vbGeneralDate.
Named Format Returns
vbGeneralDate A date and/or time. If the expression holds a date, returns a short date. If it holds a time, 2/28/99 6:01
returns a long time. If it holds both , returns both a short date and long time.
vbLongDate Day of week , Month Day , Year Sunday,February 28,1999
vbShortDate MM/DD/YY 2/28/99
vbLongTime HH:MM:SS AM/PM 6:01:24 PM
vbShortTime HH:MM( 24 hour clock ) 18:01
Example:
lblStartDate.Caption = FormatDateTime ( StartDate , vbShortDate )
lblStartTime.Caption = FormatDateTime ( "1/1/07" ,vbLongDate )