Vous êtes sur la page 1sur 2

STRING FUNCTIONS IN VB 2005/2008 The String Class

The String class implements the String data type, which is one of the richest data types in terms of the members it exposes. We have used strings extensively in earlier chapters, but this is a formal discussion of the String data type and all of the functionality it exposes. To create a new instance of the String class, you simply declare a variable of the String type. You can also initialize it by assigning to the corresponding variable a text value.

REPLACE() Is used to replace specific word in a given string.


Dim title As String = Mastering VB.NET Dim newTitle As String newTitle = title.Replace(VB, Visual Basic)

Copy() The Copy method, for example, accepts as argument the string to be copied and doesnt require an instance of the class:
Dim s1, s2 As String s1 = This is a string s2 = String.Copy(s1)

Length

The Length property returns the number of characters in the string, and its read-only. To find out the number of characters in a string variable, use the following statement:
Dim title As String = "Mastering VB.NET" Dim x As Integer x = title.Length MsgBox(x)

Compare

This method compares two strings and returns a negative value if the first string is less than the second, a positive value if the second string is less than the first, and zero if the two strings are equal. The Compare method is overloaded, and the first two arguments are always the two strings to be compared. The simplest form of the method accepts two strings as arguments:
String.Compare(str1, str2)

Dim title As String = "Mastering VB.NET" Dim x As String = "Mastering visual.Net" Dim y As Integer y = String.Compare(title, x) MsgBox(y)

Concat

This method concatenates two or more strings (places them one after the other) and forms a new string. The simpler form of the Concat method has the following syntax and its equivalent to the & operator:
newString = String.Concat(string1, string2)

Dim title As String = "Mastering VB.NET" Dim x As String = "mastering java" Dim y As String y = String.Concat(title, x) MsgBox(y)

ToUpper and ToLower


ToUpper and ToLower functions are used to convert the string to upper and lower case respectively

Dim title As String = "Mastering VB.NET" Dim y, Z As String y = title.ToUpper Z = title.ToLower MsgBox(y) MsgBox(Z)

Insert
The Insert method inserts one or more characters at a specified location in a string and returns the new string. The syntax of the Insert method is newString = str.Insert(startIndex, subString) Dim Zip As String = CA93010 Dim StateZip As String StateZip = Zip.Insert(2, -) MsgBox(StateZip)

The StateZip string variable will become CA-93010 after the execution of these statements
Date

The Date property returns the date from a date/time value and sets the time to midnight. The statements:
Dim date1 As Date date1 = Now() MsgBox(date1)

will print something like the following values in the Output window:
5/29/2001 2:30:17 PM More Date Functions
MsgBox("The MsgBox("The MsgBox("The MsgBox("The current time is " & Date.Now.TimeOfDay.ToString) hour is " & Date.Now.Hour) minute is " & Date.Now.Minute) second is " & Date.Now.Second)

Vous aimerez peut-être aussi