Parag’s Den

Dedicated to my adored friends…..

05- Strings

Posted by paragdongre on June 4, 2008


 

Text Variables

String variables are an entirely different form of variables. As you may have noticed, all the other variables that were mentioned in the previous chapter have something to do with numbers. String variables deal with text. When you deal with text variables, the commands and the methods associated with them are entirely different. For example, when was the last time that you have heard someone multiply letters? What is the result when you multiply the letter “k” with the letter “n” ? When you work with text variables or string variables, make sure that you do not confuse the numerical manipulation commands with the string or textual manipulation commands.

Declaring the Text Variable or the String Variable

Simply type in the code below to declare a string variable in your program.

 

Dim Jordan as String

 

OR

 

Dim [name of variable] as String

 

Blanks

Sometimes you will encounter blanks with text variables. For example, if a user enters the name ” Jordan” instead of “Jordan,” there will be a space or a blank in front of it. If you want to rid the blanks at the start and at the end of text entered by the user then you can use the trim method. Simply use the code as illustrated below to trim any string variables.

 
        Dim Name As String
 
        Name = Trim(TextBox1.Text)
 
        MsgBox (Name)
               

As you can see, simply enter the text in the Trim() function and the spaces at the start and at the end of the text variable will be gone.

Note: Sometimes users will forget to put an entry in a textbox if you ask them to. In order to recognize blanks, simply use “”. Note that ” ” is different from “” because the former has a space bar entry while the latter has nothing in it. Visual Basic .NET recognizes the difference between the two.

String Position

With VB .NET, you can locate a string within a string. The command used to identify a string within a string is the InStr() command. See the code below and see how it works.

 
        Dim Result As Integer
 
        Result = InStr("Display", "p")
 
        MsgBox(Result)
 

The result returns a value of 4 because “p” is the fourth occurrence on the string. Try changing “p” to “isp” and see what happens. The general structure of the InStr() command is shown below.

 
Instr("[string to look at]","[string to be look for]")

 

Equal Command with Text

If you want text to be represented within a variable then you can use the equals command. See the code below and learn how the equals command is used.

 
        Dim Represent As String
 
        Represent = "This is the result."
 
        MsgBox(Represent)
               

The main structure when using the equals command is as follows.

 
[variable] = "[text]"
 

The variable will then obtain the value of the text.

Working with Text

Joining Text

Aside from using the “=” sign from numerical values, you can also use the “+” sign too. The “+” sign is used to join text together or to concatenate them. If you want to join text together, simply use the “+” sign as seen below.

 
Dim Result As String
 
Result = "Sun" + "day"
 
MsgBox(Result)
               

The result in the message box is the word “Sunday”. Try joining different kinds of texts together. The basic structure for joining text is as follows.

 [first string] + [second string] 

Replacing Text

If you want to replace a certain string or substring then you can use the Replace() command. See the code below and learn how it is used.

 
Dim Represent As String
 
Represent = "Display".Replace("Display", "Screen")
 
MsgBox(Represent)
 

The entire word “Display” is replaced with the word “Screen”. Try changing Replace(“Display”, “Screen”) with Replace(“ispl”,””) and see what happens. The general structure of the Replace command is as follows.

"[string to be looked at]".Replace("[string to be replaced]","[string to replace it]")
 

Inserting Text

If you wish to insert a string of text into an existing string then you can use the Insert() command. See the code below and learn how it is used.

 
        Dim Represent As String
 
        Represent = "Day".Insert(1, "ispl")
 
        MsgBox(Represent)
 

The result that is displayed in the message box is “Display”. Note that the first character in the String is “D” and that is why the “ispl” string was inserted after the character “D”. Try changing Insert(1, “ispl”) to Insert(2, “ispl”) and see what happens. The general structure of the Insert command is as follows.

“[string to be looked at]”.Insert([nth character where string will be inserted], “[string to be inserted]”)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: