Monday, November 22, 2010

Visual Basic 6 Tutorial Remove Spaces in a string using Trim

,
In this topic I am going to show you how to remove left space, right space, right and left spaces and all spaces in a string in Visual Basic 6.

In removing spaces in a string, all you need to do is to use “Trim” and “Replace” function. This is very important if you are dealing with databases.

Application:

If you have an input let say Company Name and you want the Company Name to be unique which mean no duplicate allowed. If you are not going to trim your input the tendency is it will create an error or the “no duplicate rule” will not be applied. Here is the reason why:

Assuming I already have Google Company in my database and I want no duplicate, if I input “_Google_Company_ _” it will also be accepted in the database because by putting spaces in the first and the last part of the string tells that they are not equal. To make it equal you have to use Trim.

Trim$(Input.text)

This will remove spaces both first and last part of the string. Here is the code.


Private Sub Command1_Click()
Text2.Text = LTrim$(Text1.Text) 'This will remove Left space or the first space
Text3.Text = RTrim$(Text1.Text) 'This will remove right space or the last space
Text4.Text = Trim$(Text1.Text) 'This will remove space both first and last
Text5.Text = Replace(Text1.Text, " ", "") 'This will remove all the spaces
End Sub
You can download the whole program here.

Screenshot:


Remember, this tutorial is very important in storing and retrieving data from database. This will help you compare two strings properly.

Microsoft Visual Basic Professional 6.0 with Plus Pack Microsoft Visual Basic 6.0 Learning Edition Microsoft Visual Studio 6.0 Professional Edition Microsoft Visual Basic 6.0 Enterprise Edition Development System Microsoft Visual Basic Professional Edition Mastering Enterprise Development Using Microsoft Visual Basic 6.0

1 comments:

  • March 5, 2013 at 2:36 AM
    Unknown says:

    can you make an automatic space remover?

    delete

Post a Comment

 

Topic Central Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger