visual 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.

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)

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

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.


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.


The resulting three character string is 'Wel'

If we want to extract the characters from the middle, use the following statement.


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.


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))
In the above statement, n2 specifies that two decimal places are to be considered.

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")

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
    DEBIT_NOTE_CAT, PrivateTransactionCat) > 0
ParamsStoredProcedure = "GetPayRecAccs"
	PrivateTransactionCat) > 0
ParamsStoredProcedure = "GetCashAccs"
	PrivateTransactionCat) > 0
ParamsStoredProcedure = "GetBankAccs"
End Select
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.

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
		PrivateTransactionCat = "c"
pos = strTransactions.IndexOf(PrivateTransactionCat)
If (pos > -1) Then
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
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.

cover image of azure cloud book

Develop Azure Applications

Azure Cloud is gaining popularity and is competing with Amazon Elastic Cloud and Google Cloud for a spot in the billion dollar Cloud computing space.

With this book learn how to use - Table (an Azure Table is different from a SQL Server Table), Queue, Blob storage services, use your local machine to create an Application and a Service, how Azure splits a Table to achieve Load Balancing, Scalability based on the Partition Key, AppFabric Service Bus, Access Control Service, role of Fabric Controller, features of Azure SQL Database and Content Delivery Network.

Many have mastered Cloud Programming and are enjoying the benefits. Why not you?

View Highlights » View Contents »
cover image of foundation database programming book

Create your Own Accounting Software

A foundation book to Master Accounting Software Development, VB.Net, C# and OOPs programming. Detailed explanation of the Accounting domain for programmers is included. SQL and ADO.Net is also covered in detail.

This book gives you a strong foundation in Application Development.

A thorough understanding of the contents will enable you to grasp the topics covered in the accompanying books quite easily. This is because, the accounting domain is dealt with great detail in this book.

The ideal book to get started with application development in the .Net world.

View Highlights » View Contents »

cover image of book

Create your Own WEB ERP Software using ASP.Net

This book opens you to the Web. Using ASP.Net, C# and SQL Server as the tools, you will learn to build a Multi Tier Web Application as recommended by Microsoft. You will learn implementing inline GridView control, Transaction Management for a layered Web Application, build Data Access component and a Business Layer component.

Opens you to a world of opportunities. You will be able to develop a Web ERP application including all the five modules and learn how to use the TreeView control and GridView control to implement transactions such as Purchase Invoice, Sales Order and Sales Invoice.

View Highlights » View Contents »
cover image of entity framework book

ASP.Net MVC 5 Application Development Using Entity Framework 6 and VS 2015

Entity frameworks is the latest buzz in .Net programming and is the data access technology for Microsoft OS based hand held devices and web applications. Learn to integrate EF into an ASP.Net Application and MVC application. Create, explore and query the Entity Data Model. Learn how to use ObjectDataSource control and GridView control in an ASP.Net Application.

Also, learn to build Three-Tier ASP.Net application, create a business-class, implement methods in the class to perform CRUD operations and bind a GridView to the ObjectDataSource control. Learn how to handle concurrency and managing Transactions.

View Contents »