A string is a sequence of characters. We use "" to mark its beginning and end. The need for strings arises when we need to display a message to the user. In any database software we develop, we often use the statement MessageBox.Show ('Welcome'). This is a common example of using strings. In Visual Basic 2005, strings are implemented as a class. How to declare a String Variable in visual basic 2005 Dim strName As String How to assign a string to a string variable StrName = "John" How to Display a string MessageBox.Show (strName)
There are many occasions when we need to concatenate strings. Suppose we would like to join two strings - 'Welcome' and 'to .Net Programming'. The code below accomplishes this requirement. Declare string variables
The string 'Welcome to .Net Programming' is displayed. How to get a length of a string in a visual basic 2005 We use the Length property of the string to calculate the length of the string. This property returns integer value that is equal to number of characters in the string. StrName.Length
The Substring method returns the set of characters from any position in the string. We will mention the starting position and number of characters to extract. If we want to extract the characters from the starting position, use the following statement.
We need to pass two parameters to the Format method. The first parameter is the format that we want to be displayed and the second parameter is the value we want to format. This method is used mostly to display floating point numbers with digits in the right places. Below is the code for declaring a floating point number.
This method takes two parameters, the first one is the string to be replaced and the second is the new string which will replace the first. This method returns the new string and displays the 'Welcome to Visual Basic 2005 Programmers' string.
Instr function is used to determine if the contents of one string are present in another string. Given below is the code for searching whether a particular string is present in another string.The instr() function call have the parameters which are listed below.
IndexOf() is a new string method provided in visual basic 2005. It is different in the way it returns the result. IndexOf method is used to extract the first instance of a substring from a string. IndexOf can be used to return the starting position of the substring. If the string is located, IndexOf also returns the first character located at position 0. IndexOf returns -1 if the substring is not found. In the below code, the constants PURCHASE_CAT, SALES_CAT and CASH_VOUCHER_CAT are concatenated into the string strTransactions. The string PrivateTransactionCat is assigned to a character 'c'. The IndexOf() method is used to determine the position of 'c' in the string strTransactions.
This method .ToUpper belongs to the String Class. It converts the string to uppercase. In the below example, the function IsDuplicateCode Checks whether account exists in the Dataset. The code entered by the user is converted to upper case and then checked with the account in the dataset.
The System.String class is the class provided in .Net for manipulating strings. Using System.String, we can search for substrings, change the case of a string, determine a string's length, compare two strings, and split strings. In the visual basic 6.0 and earlier Strings were categorized under datatype.
Strings are an important feature of any software application package / database applications. String manipulation is essential, and one instance where we commonly use strings is when we accept data from users. So, handing of strings becomes extremely important requirement for application development. In all the books of erp software development books, string manipulation is used to perform critical tasks.
Another clever usage of strings is displaying messages when we debug an application.
At different places where a program returns values and results of functions, procedures and methods, we can place message boxes to know what is the state of the program. By placing appropriate message boxes, we can identify the bugs in the program. We also place meaningful messages in error handling, thereby prompting the user with alternative actions.