Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1.

WITH AID OF EXAMPLES, demonstrate your understanding on the following


string manipulation methods and properties as in VB

Concatenation operators join multi strings into a single string. There are two
types of concatenation operators + and & both carry basic concatenation
operations.
Dim x As String = “mic” & “ro” & “soft”
Dim y As String = “mic”&”ro”&”soft”
‘the preceeding statemnts sets both x and y to Microsoft
these operators can also concatenate string variables as follows
Dim a As String = “abc”
Dim d As String = “def”
Dim z As String = a & d
Dim w As String = “a + d”
The preceeding statements set both z and w to “abcdef”

LENGTH PROPERTY With the Length property on the String type, you can determine this

count fast. Length is useful in many VB.NET programs. Its implementation is also interesting

to examine. We explore the performance accessing Length in a for-loop. A String's length

could be computed once and stored as a field Or it could be calculated in a loop every time

the property is accessed. In VB.NET, the value is stored, which makes getting the length of

any string equally fast. If the Length were to be computed each time, a loop over every

character that tested against the bound Length would be slow. Performance, for-loop. It is

typically faster to use Length directly in the bounds of a for-loop. The JIT compiler can make

optimizations to help char access.

VB.NET program that uses Length property

Module Module1

Sub Main() ' Get length of string.


Dim value As String = "dotnet"
Dim length As Integer = value.Length
Console.WriteLine("{0}={1}", value, length) ' Append a string. ' ... Then get the length of the
newly-created string.
value += "field" length = value.Length
Console.WriteLine("{0}={1}", value, length)
End Sub

End Module

Output dotnet=6 dotnetfield=11

This program shows the Length property in action. The length of the String "dotnet" is found

to be 6 characters. After another 5 characters are appended, the length is now 11 characters.

A String cannot be directly changed. So when "field" is appended to the String, a whole new

String is created. During String creation, the length value is stored. The String will thus

always have the same length.

TO UPPER converts all characters to uppercase characters. It causes a copy to be made of

the VB.NET String, which is returned. This console program shows the result of ToUpper on

the input String "abc123". "abc" are the only characters that were changed. The non-

lowercase letters are not changed. Characters that are already uppercase are not changed by

the ToUpper Function.

VB.NET program that calls ToUpper on String

Module Module1

Sub Main()

Dim value1 As String = "abc123"

Dim upper1 As String =

value1.ToUpper()

Console.WriteLine(upper1)

End Sub

End Module

Output ABC123
We can determine whether a string is already uppercase. We can use ToUpper and then

compare the original String. If they are equal, the string was already uppercased. However

this is not the most efficient way. A faster way uses a For-Each loop and then the

Char.IsLower function.

For Each, For

 If a Char is lowercase, the String is not already uppercase. The code returns False early at

that point.

VB.NET program that tests for uppercase strings

Module Module1

Function IsUppercase(ByRef value As String) As Boolean


' Loop over all characters in the string. ' ... Return false is one is lowercase. ' Otherwise,
return true (all letters are uppercase).

For Each letter As Char In value

If Char.IsLower(letter) Then

Return False
End If
Next

Return True
End Function

Sub Main()

Console.WriteLine("ISUPPERCASE: {0}", IsUppercase("TENDAI"))


Console.WriteLine("ISUPPERCASE: {0}", IsUppercase("Tendai"))
Console.WriteLine("ISUPPERCASE: {0}", IsUppercase(""))

End Sub

End Module

Output
ISUPPERCASE: True
ISUPPERCASE: False
ISUPPERCASE: True
TO LOWER changes Strings that contain uppercase letters. We convert all the uppercase

letters in a String to lowercase letters. With ToLower this is easy to doFunction notes. We

can also use ToLower to determine if a String is already lowercased.

VB.NET program that uses ToLower

Module Module1

Sub Main()

' Convert string to lowercase.

Dim value As String = "ABC123"

value = value.ToLower()

Console.WriteLine(value)

' See if a String is already lowercase.

Dim cat As String = "cat"

If cat = cat.ToLower()

Then Console.WriteLine("Is Lower")

End If

End Sub

End Module

Output

abc123 Is Lower

This program uses the ToLower function. In the first example, we lowercase the String

"ABC123": notice how the non-uppercase letters "123" are kept the same in the

output.Function. In the second example, we show how to use ToLower to test if a String is

already lowercased. . It is possible to use an argument to the ToLower function. This is of
type System.Globalization.CultureInfo. This influences how non-ASCII characters are

converted.

TRIM Returns a string containing a copy of a specified string with no leading spaces

(LTrim), no trailing spaces (RTrim), or no leading or trailing spaces (Trim). A string

containing a copy of a specified string with no leading spaces (LTrim), no trailing spaces

(RTrim), or no leading or trailing spaces (Trim).This example uses the LTrim function to

strip leading spaces and the RTrim function to strip trailing spaces from a string variable. It

uses the Trim function to strip both types of spaces.

' Initializes string.


Dim testString As String = " <-Trim-> "
Dim trimString As String
' Returns "<-Trim-> ".
trimString = LTrim(testString)
' Returns " <-Trim->".
trimString = RTrim(testString)
' Returns "<-Trim->".
trimString = LTrim(RTrim(testString))
' Using the Trim function alone achieves the same result.
' Returns "<-Trim->".
trimString = Trim(testString)
The LTrim, RTrim, and Trim functions remove spaces from the ends of
strings.

2. An event is a signal that informs an application that something important has occurred. For
example, when a user clicks a control on a form, the form can raise a Click event and call a
procedure that handles the event. Events also allow separate tasks to communicate. Say, for
example, that your application performs a sort task separately from the main application. If a
user cancels the sort, your application can send a cancel event instructing the sort process to
stop. You declare events within classes, structures, modules, and interfaces using
the Event keyword, as in the following example:
Event AnEvent(ByVal EventNumber As Integer)
Raising Events
An event is like a message announcing that something important has occurred. The act of
broadcasting the message is called raising the event. In Visual Basic, you raise events with
the RaiseEvent statement, as in the following example:

RaiseEvent AnEvent(EventNumber)
Events must be raised within the scope of the class, module, or structure where they are
declared. For example, a derived class cannot raise events inherited from a base class.Any
object capable of raising an event is an event sender, also known as an event source. Forms,
controls, and user-defined objects are examples of event senders.
Event handlers are procedures that are called when a corresponding event occurs. You can
use any valid subroutine with a matching signature as an event handler. You cannot use a
function as an event handler, however, because it cannot return a value to the event source.
Visual Basic uses a standard naming convention for event handlers that combines the name of
the event sender, an underscore, and the name of the event. For example, the Click event of a
button named button1 would be named Sub button1_Click.Before an event handler becomes
usable, you must first associate it with an event by using either
the Handles or AddHandler statement. WithEvents and the Handles Clause
The WithEvents statement and Handles clause provide a declarative way of specifying event
handlers. An event raised by an object declared with the WithEvents keyword can be handled
by any procedure with a Handles statement for that event, as shown in the following example:

' Declare a WithEvents variable.


Dim WithEvents EClass As New EventClass

' Call the method that raises the object's events.


Sub TestEvents()
EClass.RaiseEvents()
End Sub

' Declare an event handler that handles multiple events.


Sub EClass_EventHandler() Handles EClass.XEvent, EClass.YEvent
MsgBox("Received Event.")
End Sub

Class EventClass
Public Event XEvent()
Public Event YEvent()
' RaiseEvents raises both events.
Sub RaiseEvents()
RaiseEvent XEvent()
RaiseEvent YEvent()
End Sub
End Class
The WithEvents statement and the Handles clause are often the best choice for event handlers
because the declarative syntax they use makes event handling easier to code, read and debug.
However, be aware of the following limitations on the use of WithEvents variables:
 You cannot use a WithEvents variable as an object variable. That is, you cannot
declare it as Object—you must specify the class name when you declare the variable.
 Because shared events are not tied to class instances, you cannot use WithEvents to
declaratively handle shared events. Similarly, you cannot use WithEvents or Handles to
handle events from a Structure. In both cases, you can use the AddHandler statement to
handle those events.
 You cannot create arrays of WithEvents variables.
WithEvents variables allow a single event handler to handle one or more kind of event, or
one or more event handlers to handle the same kind of event. Although the Handles clause is
the standard way of associating an event with an event handler, it is limited to associating
events with event handlers at compile time. In some cases, such as with events associated
with forms or controls, Visual Basic automatically stubs out an empty event handler and
associates it with an event. For example, when you double-click a command button on a form
in design mode, Visual Basic creates an empty event handler and a WithEvents variable for
the command button, as in the following code:
Friend WithEvents Button1 As System.Windows.Forms.Button
Protected Sub Button1_Click() Handles Button1.Click
End Sub

You might also like