Pages

Wednesday, April 27, 2016

VB.Net Chapter 6 - String Manipulation

Chapter 6 - String Manipulation



In This Chapter
• Learn how to manipulate Strings

String manipulation is an important part of programming because it helps to process data that come in the form of non-numeric types such as name, address, city, book title and etc.

6.1 String Manipulation Using + and & signs. 

Strings can be manipulated using the  & sign and the + sign, both perform the string concatenation which means combining two or more smaller strings into a larger string. For example, we can join "Visual" and "Basic" into "Visual Basic" using "Visual"&"Basic" or "Visual "+"Basic", as shown in the example below

Example 6.1


Public Class Form1 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button1.Click 
Dim text1, text2, text3 As String 
text1 = "Visual" 
text2 = "Basic" 
text3 = text1 + text2 
Label1.Text = text3 
End Sub 
End Class


The line text3=text1+ text2 can be replaced by text3=text1 & text2 and produced the same output. However, if one of the variables is declared as numeric data type, you cannot use the + sign, you can only use the  & sign.

Example 6.2 


Dim text1, text3 as string 
Dim Text2 As Integer 
text1 = "Visual" 
text2=22 
text3=text1+text2 
Label1.Text = text3


This code will produce an error because of data mismatch. However, using & instead of + will be all right.

Example 6.3 


Dim text1, text3 as string 
Dim Text2 As Integer 
text1 = "Visual" 
text2=22 
text3=text1 & text2 
Label1.Text = text3

You can combine more than two strings to form a larger string, like the following example:

Example 6.4 


Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text1, text2, text3, text4, text5, text6 As String 
text1 = "Welcome"
text2 = "to"
text3 = "Visual"
text4 = "Basic"
text5 = "2008"
text6 = text1 + text2 + text3
Label1.Text = text4  
End Sub End Class

Running the above program will produce the following screen shot.


Figure 6.1: The Screen Shot 



6.2 String Manipulation Using VB2010 Built-in Functions 

A function is similar to a normal procedure but the main purpose of the function is to accept a certain input and return a value, which is passed on to the main program to finish the execution. VB2010 has numerous built-in string manipulation functions but we will only discuss a few here. You will learn more about these functions in later Chapters.

6.2 (a) the Len Function 

The length function returns an integer value that is the length of a phrase or a sentence, including the empty spaces. The format is

Len ("Phrase") 

For example,

Len (Visual Basic) = 12 and  
Len (welcome to VB tutorial) = 22 


Example 6.5 


Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
Label1.Text = Len(TextBox1.Text)  
End Sub
End Class

The output:


Figure 6.2 



6.2(b) the Right  Function 

The Right function extracts the right portion of a phrase. The format for Visual Basic 6 is

Right ("Phrase", n)

Where n is the starting position from the right of the phase where the portion of the phrase is going to be extracted.  For example,

Right("Visual Basic", 4) = asic 

However, this format is not applicable in VB2010. In VB2010, we need to use the following format

Microsoft.VisualBasic.Right("Phrase",n) 

Example 6.6 


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text1 As String  
text1 = TextBox1.Text  
Label1.Text = Microsoft.VisualBasic.Right(text1, 4) 
End Sub

The above program will return four right most characters of the phrase entered into the textbox, as shown in Figure 6.3

Figure 6.3 



*The reason of using the full reference is because many objects have the Right properties so using Right on its own will make it ambiguous to VB2010.

6.2(c) the Left Function 

The Left function extract the left portion of a phrase. The format is

Microsoft.VisualBasic.Left("Phrase",n) 

Where n is the starting position from the left of the phase where the portion of the phrase is going to be extracted.  For example,

Microsoft.VisualBasic.Left (“Visual Basic”, 4) = Visu

We will learn more about string manipulation function in Chapter 11.

Summary 
• In section 6.1, you learned how to manipulate strings using + and & signs. The + and & signs are used to join up two strings. 
• In section 6.2, you learned how to use string manipulation functions. Among the functions are Len, Right and  Left, 

0 comments:

Post a Comment