Sie sind auf Seite 1von 13

String Manipulation

Part 3
Objectives

 Understand String Manipulation Techniques


including:
– Pad Methods
– With Methods
– Contains Method
– IndexOf Method
Understand String Manipulation
Techniques– Pad Methods

 string.PadLeft(length[, character])
 string.PadRight(length[, character])

 To insert characters at either the beginning or


end of a string, you can use the PadLeft and
PadRight methods respectively. Both
methods pad the string with a character until
the string is a specified length.
Understand String Manipulation
Techniques– Pad Methods

 string.PadLeft(length[, character])
 string.PadRight(length[, character])

Dim dblNetPay as Double = 767.89


Me.lblNetPay.Text = dblNetPay.TOString(“C2”).PadLeft(15, “*”)

Assigns “********$767.89” to the lblNetPay Text


property.
Understand String Manipulation
Techniques– With Methods

 string.StartsWith(substring)
 string.EndsWith(substring)

 In some applications, you might need to determine


whether a string begins or ends with a specific
character or characters. For example, you may need
to determine whether a phone number entered by
the user begins with area code “312”. Or you may
need to determine whether a tax rate entered by the
user ends with a percent sign. The With methods
allows you to do this.
Understand String Manipulation
Techniques– With Methods

 string.StartsWith(substring)
 string.EndsWith(substring)

Dim dblNetPay as Double


dblNetPay = me.txtNetPay.text
If dblNetPay.StartsWith(“$”) Then
dblNetPay = dblNetPay.TrimStart(“$”)
End If

Determines whether the string stored in the dblNetPay


variable begins with the “$” sign. If it does, the dollar sign is
removed.
Understand String Manipulation
Techniques– With Method

 string.StartsWith(substring)
 string.EndsWith(substring)

Dim strCityState as String


strCityState = me.txtCityState.text.ToUpper
If strCityState.EndsWith(“CA”) Then
lblState.Text = “California Customer”
End If

Determines whether the string stored in the strCityState


variable ends with “CA”. If it does, the string “California Customer”
is assigned the lblState’s Text property.
Understand String Manipulation
Techniques– Contains Method

 string.Contains(substring)

 You can use the Contains method to search a string


to determine whether it contains a specific sequence
of characters. For example, you can use the method
to determine whether the area code “(312)” appears
in a phone number, or whether the string name “Elm
Street” appears in an address. The Contains clause
returns a Boolean value (true or false).
Understand String Manipulation
Techniques– Contains Method

 string.Contains(substring)

Dim strAddress as String


Dim blnContained as Boolean
strAddress = “555 Elm Street, Glendale, CA”
blnContained = strAddress.Contains(“Elm Street”)

Assigns the boolean value True to the isContained


variable, because “Elm Street” appears in the
strAddress variable.
Understand String Manipulation
Techniques– Contains Method

 string.Contains(substring)

Dim strPhone as String = “(312) 999-9999”


If strPhone.Contains(“(312)”) Then

The If…Then…Else statement’s condition


evaluates to True, because “(312)” appears in
the phone variable.
Understand String Manipulation
Techniques – IndexOf Method

 string.IndexOf(substring[,startIndex])

 In addition to using the Contains method to search


for a substring anywhere within a string, you also can
use the IndexOf method. Unlike the Contains
method, the IndexOf method returns an integer that
indicates whether the string contains a substring.
The IndexOf method returns an integer that
represents the location of the substring. If it can not
find the substring, a -1 is returned.
Understand String Manipulation
Techniques – IndexOf Method

 string.IndexOf(substring[,startIndex])

Dim strMessage as String = “Have a nice day”


Dim intIndexNum as Integer
intIndexNum = strMessage.IndexOf(“nice”, 0)

Assigns the number 7 to the intIndexNum variable


Understand String Manipulation
Techniques – IndexOf Method

 string.IndexOf(substring[,startIndex])

Dim strMessage as String = “Have a nice day”


Dim intIndexNum as Integer
intIndexNum = strMessage.IndexOf(“nice”, 8)

Assigns the number -1 to the intIndexNum variable

Das könnte Ihnen auch gefallen