visual basic.net 2005 Strings
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.
Declare string variables
The string 'Welcome to .Net Programming' is displayed.
StrName.Length
The resulting three character string is 'Wel'
If we want to extract the characters from the middle, use the following statement.
MessageBox.Show(str1.Substring(2,3))
The resulting three character string is 'lco'
If we mention only one parameter, the substring method starts at the given position and extract the complete string till the end.
MessageBox.Show(str1.Substring(3))
The resulting character string is 'come'
The required result is calculated and displayed with the places after the decimal running into several digits. For practical purposes we will display only upto two digits after the decimal.
In the above statement, n2 specifies that two decimal places are to be considered.
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.
The instr() function call have the parameters which are listed below.
Public Shared Function InStr(ByVal Start As Integer, ByVal String1 As String, ByVal String2 As String) As Integer
With the start parameter we can specify the position on the string we begin to look at. Note that the Start Parameter starts at 1, and not 0. String1 specifies the target string to look at. String2 refers to the string which is in consideration.
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.
How to declare a String Variable in visual basic 2005
Dim strName As StringHow to assign a string to a string variable
StrName = "John"How to Display a string
MessageBox.Show (strName)Concatenation of Strings
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
Dim str1 As String Dim str2 As String Dim strResult As String Set the string values str1="Welcome" str2=" to .Net Programming" Concatenate the strings strResult=str1 & str2 Displaying a string MessageBox.Show (strResult)
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
How to get a set of characters that appears from start, end and middle of a string
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.str1="Welcome" MessageBox.Show(str1.Substring(0,3))
The resulting three character string is 'Wel'
If we want to extract the characters from the middle, use the following statement.
MessageBox.Show(str1.Substring(2,3))
The resulting three character string is 'lco'
If we mention only one parameter, the substring method starts at the given position and extract the complete string till the end.
MessageBox.Show(str1.Substring(3))
The resulting character string is 'come'
Formatting Strings using Format method
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.Dim dblBal As Double dbl Bal = 3456.879 dbl Bal *= 5.38
The required result is calculated and displayed with the places after the decimal running into several digits. For practical purposes we will display only upto two digits after the decimal.
MessageBox.Show("the balance is:" & String.Format("{0:n2}", dblBal))
Replacing SubStrings
Replacing strings with another string is a common requirement in database programming. Take the example where we want to replace 'Visual Basic' with 'Visual Basic 2005'.Dim str As String = "Welcome to Visual Basic Programmers" Dim strResult As String strResult = str.Replace("Visual Basic", "Visual Basic 2005") MessageBox.Show(strResult)
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
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.Select Case vbn Case InStr(1, PURCHASE_CAT + SALES_CAT + CREDIT_NOTE_CAT + DEBIT_NOTE_CAT, PrivateTransactionCat) > 0 ParamsStoredProcedure = "GetPayRecAccs" Case InStr(1, CASH_VOUCHER_CAT + CASH_RECEIPT_CAT, PrivateTransactionCat) > 0 ParamsStoredProcedure = "GetCashAccs" Case InStr(1, CHEQ_VOUCHER_CAT + CHEQ_RECEIPT_CAT, PrivateTransactionCat) > 0 ParamsStoredProcedure = "GetBankAccs" End Select
Public Shared Function InStr(ByVal Start As Integer, ByVal String1 As String, ByVal String2 As String) As Integer
With the start parameter we can specify the position on the string we begin to look at. Note that the Start Parameter starts at 1, and not 0. String1 specifies the target string to look at. String2 refers to the string which is in consideration.
Indexof() method of String Class in visual basic 2005
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.Public Class Form4 Private Const PURCHASE_CAT = "a" Private Const SALES_CAT = "b" Private Const CASH_VOUCHER_CAT = "c" Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strTransactions As String Dim PrivateTransactionCat As String Dim pos As Integer = 0 strTransactions = PURCHASE_CAT + SALES_CAT + CASH_VOUCHER_CAT PrivateTransactionCat = "c" pos = strTransactions.IndexOf(PrivateTransactionCat) If (pos > -1) Then MessageBox.Show(pos.ToString()) End If End Sub End Class
.ToUpper method of String class
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.Private Function IsDuplicateCode() As Boolean 'This Function Checks whether account exists in the Dataset Dim lsuccess As Boolean lsuccess = False Dim i As Integer For i = 0 To (myAllAccountDataset.Tables("AccountsTable").Rows.Count()- 1) If LTrim(RTrim(myAllAccountDataset.Tables("AccountsTable").Rows(i)(0))) = LTrim(RTrim(mCode.Text.ToUpper)) Then lsuccess = True Exit For End If Next Return lsuccess End Function
System.String class
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.Why are strings important in visual basic 2005 application software development?
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.
Visual Basic Articles
- Connection string for connecting to data sources
- constructors in Visual Basic.Net
- Constants in Visual basic 2005
- Visual basic .net Enumerations
- Visual basic 2005 Formatting Numbers
- Type conversion in Visual basic .net
- Visual basic 2005 Arrays
- Visual basic 2005 Variables
- classes in visual basic.net 2005
People also viewed:
Create your own ERP Software using ASP Core>>
Create your own Azure SOM Software>>
Create your own ERP Software using ASP .Net and SQL>>
Create your own Accounting Software using C# >>
Create your own SOM using Entity Framework MVC >>





Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net