CopyTo
CopyTo
The CopyTo method is a little more complex than the Copy method, but it works harder to give much more
manipulation power. The CopyTo method takes a character at the source position of a String, at your selected
index value, and then copies the character to a destination position in a character Array. The base syntax is as
follows:
Str1.CopyTo(int1, myArray, int2, int3)
The character at int1 is the starting point or source index in the source Stringin the preceding example, the
source is Str1. The parameter myArray is the destination Array you must provide. Finally, Int2 is the
starting index or destination index in the target Array and Int3 is the number of characters to copy from the
source String, as shown in the following example:
Dim intI As Integer
Dim str1 As String = "Houston, we have a problem."
Dim myArray(5) As Char
str1.CopyTo(0, myArray, 0, 4)
str1.CopyTo(10, myArray, 4, 1)
For intI = 0 To 4
Console.WriteLine(myArray(intI))
Next I
In the preceding code example, we have declared an array (myArray) of type Char to hold five characters.
Then we copy four characters into myArray starting at index 0 and ending at index 3. Next, using str1, we
copy character "e" in position 10 in the String to the index position 5 in the Array. The characters copied into
the Array are "h," "o," "u," "s," and "e."
Finally, to write the Array contents to the console, we used a For . . . Next loop (refer to Chapter 6), which
loops four times to output the characters and display the following:
h
o
u
s
e
EndsWith, StartsWith
The EndsWith and StartsWith methods are useful for simple checks on whether certain Strings or even
single characters appear at the beginning or end of Strings. You will receive True or False if the String you
are hoping to find is or is not at the end or beginning of your String. Let's check out this useful method:
Str.EndsWith()
Str.StartsWith()
Have a look at the following example:
Dim str1 As String = "Houston, we have a problem."
If str1.EndsWith("problem") Then
console.WriteLine("true")
End If
499