VB.Net/Language Basics/String
Содержание
- 1 Combine Insert and IndexOf
- 2 Compare String: case sensitive and not sensitive
- 3 Compare two Strings
- 4 Comparing strings
- 5 Concatenate Strings
- 6 Concatenate String with Integer into one String
- 7 Concatenate the strings
- 8 Concatenate the strings and display the results
- 9 Convert to Upper case and Lower Case
- 10 Demonstrating StartsWith and EndsWith methods
- 11 Display the first part of a string
- 12 Display the length of the string
- 13 Get sub string
- 14 IndexOfAny to find first occurrence of character in array
- 15 IndexOf to locate a substring in a string
- 16 LastIndexOfAny to find first occurrence of character in array
- 17 LastIndexOf to find a character in a string
- 18 LastIndexOf to find a substring in a string
- 19 Replace the string occurance
- 20 String class Concat method
- 21 String compare
- 22 String Constructor
- 23 String Copy
- 24 String ends with
- 25 String.Equals Demo
- 26 String IndexOf and Insert
- 27 String IndexOf and Insert at a position
- 28 String Insert
- 29 String InStr
- 30 String Object: =, Is and Mid
- 31 String PadLeft
- 32 String properties Length and Chars and method CopyTo of class String
- 33 String Replace Demo
- 34 String: set value and display
- 35 String Split
- 36 String: Substring, LastIndexOf
- 37 String Substring method
- 38 String ToCharArray method
- 39 String ToLower and ToUpper
- 40 String ToString method
- 41 String Trim method
- 42 Sub string with only one parameter
- 43 The length of the string
- 44 Trim String space
- 45 Two ways to concatenate strings
- 46 Two ways to copy strings
- 47 Use String.Equals methods
- 48 Use the Mid function to replace within the string
- 49 Using String searching methods: IndexOf
Combine Insert and IndexOf
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
Dim s3 As String = "Liberty Associates, Inc. provides "
s3 = s3 & "custom .NET development"
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
Dim location As Integer = s3.IndexOf("provides")
" you can combine the two as follows:
Dim s11 As String = _
s3.Insert(s3.IndexOf("provides"), "usually ")
Console.WriteLine("s11: {0}", s11)
End Sub
End Class
Compare String: case sensitive and not sensitive
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
Dim result As Integer
result = String.rupare(s1, s2)
Console.WriteLine( _
"compare s1: {0}, s2: {1}, result: {2}" _
& Environment.NewLine, s1, s2, result)
result = String.rupare(s1, s2, True)
Console.WriteLine("Compare insensitive. result: {0}" _
& Environment.NewLine, result)
End Sub
End Class
Compare two Strings
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
" create some Strings to work with
Dim s1 As [String] = "abcd"
Dim s2 As [String] = "ABCD"
Dim result As Integer " hold the results of comparisons
" compare two Strings, case sensitive
result = [String].rupare(s1, s2)
Console.WriteLine( _
"compare s1: {0}, s2: {1}, result: {2}" _
& Environment.NewLine, s1, s2, result)
" overloaded compare, takes boolean "ignore case"
"(true = ignore case)
result = [String].rupare(s1, s2, True)
Console.WriteLine("Compare insensitive. result: {0}" _
& Environment.NewLine, result)
End Sub
End Class
Comparing strings
Imports System
Public Class MainClass
Shared Sub Main()
Dim string1 As String = "hello"
Dim string2 As String = "good bye"
Dim string3 As String = "Happy Birthday"
Dim string4 As String = "happy birthday"
Dim output As String
Dim quotes As Char = ChrW(34)
Console.WriteLine( "output values of four Strings")
Console.WriteLine( "string1 = " & quotes & string1 & quotes & _
vbCrLf & "string2 = " & quotes & string2 & quotes & _
vbCrLf & "string3 = " & quotes & string3 & quotes & _
vbCrLf & "string4 = " & quotes & string4 & quotes)
Console.WriteLine(" test for equality using Equals method ")
If (string1.Equals("hello")) Then
Console.WriteLine( "string1 equals " & quotes & "hello" & quotes )
Else
Console.WriteLine( "string1 does not equal " & quotes & _
"hello" & quotes )
End If
Console.WriteLine( "test for equality with = ")
If string1 = "hello" Then
Console.WriteLine( "string1 equals " & quotes & "hello" & _
quotes )
Else
Console.WriteLine("string1 does not equal " & quotes & _
"hello" & quotes)
End If
Console.WriteLine(" test for equality comparing case ")
If (String.Equals(string3, string4)) Then
Console.WriteLine("string3 equals string4")
Else
Console.WriteLine("string3 does not equal string4" )
End If
Console.WriteLine(" test CompareTo")
Console.WriteLine("string1.rupareTo(string2) is " & _
string1.rupareTo(string2) & vbCrLf & _
"string2.rupareTo(string1) is " & _
string2.rupareTo(string1) & vbCrLf & _
"string1.rupareTo(string1) is " & _
string1.rupareTo(string1) & vbCrLf & _
"string3.rupareTo(string4) is " & _
string3.rupareTo(string4) & vbCrLf & _
"string4.rupareTo(string3) is " & _
string4.rupareTo(string3) )
End Sub " Main
End Class
Concatenate Strings
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
" concatenation method
Dim s3 As String = String.Concat(s1, s2)
Console.WriteLine("s3 concatenated from s1 and s2: {0}", s3)
" use the overloaded operator
Dim s4 As String = s1 & s2
Console.WriteLine("s4 concatenated from s1 & s2: {0}", s4)
End Sub
End Class
Concatenate String with Integer into one String
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim n As Integer
n = 27
n = n + 1
Console.WriteLine("Value of n + 1 = " & n, "Variables")
End Sub
End Class
Concatenate the strings
Imports System
Public Class MainClass
Shared Sub Main()
"Declare variables
Dim strOne As String
Dim strTwo As String
Dim strResults As String
"Set the string values
strOne = "Hello"
strTwo = ", world!"
"Concatenate the strings
strResults = strOne & strTwo
"Display the results
System.Console.WriteLine(strResults)
End Sub
End Class
Concatenate the strings and display the results
Imports System
Public Class MainClass
Shared Sub Main()
"Declare variables and set their values
Dim strOne As String = "Visual Basic "
Dim strTwo As String = "2005"
"Concatenate the strings and display the results
System.Console.WriteLine(strOne & strTwo)
End Sub
End Class
Convert to Upper case and Lower Case
Imports System
Public Class MainClass
Shared Sub Main()
Dim river As String = " www.vbex.ru "
Console.WriteLine(river.ToUpper())
Console.WriteLine(river.ToLower())
End Sub
End Class
Demonstrating StartsWith and EndsWith methods
Imports System
Public Class MainClass
Shared Sub Main()
Dim strings As String()
Dim i As Integer
Dim quotes As Char = ChrW(34)
strings = New String() {"started", "starting", _
"ended", "ending"}
Console.WriteLine(" test every string to see if it starts with "st"")
For i = 0 To strings.GetUpperBound(0)
If strings(i).StartsWith("st") Then
Console.WriteLine(quotes & strings(i) & quotes & _
" starts with " & quotes & "st" & quotes )
End If
Next
Console.WriteLine(" test every string to see if it ends with "ed"")
For i = 0 To strings.GetUpperBound(0)
If strings(i).EndsWith("ed") Then
Console.WriteLine( quotes & strings(i) & quotes & _
" ends with " & quotes & "ed" & quotes )
End If
Next
End Sub " Main
End Class
Display the first part of a string
Imports System
Public Class MainClass
Shared Sub Main()
"Display the first part of a string
System.Console.WriteLine("The length of the string in the TextBox is ".Substring(0, 12))
End Sub
End Class
Display the length of the string
Imports System
Public Class MainClass
Shared Sub Main()
"Declare variable
Dim strData As String
strData = "string value"
"Display the length of the string
System.Console.WriteLine(strData.Length & " character(s)")
End Sub
End Class
Get sub string
Imports System
Public Class MainClass
Shared Sub Main()
"Declare variable
Dim strData As String
strData = "string value"
"Display the first three characters
System.Console.WriteLine(strData.Substring(0, 3))
"Display the middle three characters
System.Console.WriteLine(strData.Substring(3, 3))
"Display the last three characters
System.Console.WriteLine(strData.Substring(strData.Length - 3))
End Sub
End Class
IndexOfAny to find first occurrence of character in array
Imports System
Public Class MainClass
Shared Sub Main()
Dim letters As String = "abcdefghijklmabcdefghijklm"
Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
Console.WriteLine("IndexOfAny to find first occurrence of character in array")
Console.WriteLine("First occurrence of ""c""," & _
" ""a"" or ""$"" is located at " & _
letters.IndexOfAny(searchLetters))
Console.WriteLine("First occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
letters.IndexOfAny(searchLetters, 7))
Console.WriteLine("First occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
letters.IndexOfAny(searchLetters, 20, 5))
End Sub " Main
End Class
IndexOf to locate a substring in a string
Imports System
Public Class MainClass
Shared Sub Main()
Dim letters As String = "abcdefghijklmabcdefghijklm"
Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
Console.WriteLine("IndexOf to locate a substring in a string")
Console.WriteLine("""def"" is located at" & _
" index " & letters.IndexOf("def"))
Console.WriteLine("""def"" is located at index " & _
letters.IndexOf("def", 7))
Console.WriteLine("""hello"" is located at index " & _
letters.IndexOf("hello", 5, 15))
End Sub " Main
End Class
LastIndexOfAny to find first occurrence of character in array
Imports System
Public Class MainClass
Shared Sub Main()
Dim letters As String = "abcdefghijklmabcdefghijklm"
Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
Console.WriteLine("LastIndexOfAny to find first occurrence of character in array")
Console.WriteLine("Last occurrence of ""c""," & _
" ""a"" or ""$"" is located at " & _
letters.LastIndexOfAny(searchLetters))
Console.WriteLine("Last occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
letters.LastIndexOfAny(searchLetters, 1))
Console.WriteLine("Last occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
letters.LastIndexOfAny(searchLetters, 25, 5))
End Sub " Main
End Class
LastIndexOf to find a character in a string
Imports System
Public Class MainClass
Shared Sub Main()
Dim letters As String = "abcdefghijklmabcdefghijklm"
Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
Console.WriteLine("LastIndexOf to find a character in a string")
Console.WriteLine("Last ""c"" is located at " & _
"index " & letters.LastIndexOf("c"c))
Console.WriteLine("Last ""a"" is located at index " & _
letters.LastIndexOf("a"c, 25))
Console.WriteLine("Last ""$"" is located at index " & _
letters.LastIndexOf("$"c, 15, 5))
End Sub " Main
End Class
LastIndexOf to find a substring in a string
Imports System
Public Class MainClass
Shared Sub Main()
Dim letters As String = "abcdefghijklmabcdefghijklm"
Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
Console.WriteLine("LastIndexOf to find a substring in a string")
Console.WriteLine("Last ""def"" is located " & _
"at index " & letters.LastIndexOf("def"))
Console.WriteLine("Last ""def"" is located at " & _
letters.LastIndexOf("def", 25))
Console.WriteLine("Last ""hello"" is located at " & _
"index " & letters.LastIndexOf("hello", 20, 15))
End Sub " Main
End Class
Replace the string occurance
Imports System
Public Class MainClass
Shared Sub Main()
"Declare variables
Dim strData As String
Dim strNewData As String
"Get the text from the TextBox
strData = "Hello You"
"Replace the string occurance
strNewData = strData.Replace("Hello", "Goodbye")
"Display the new string
System.Console.WriteLine(strNewData)
End Sub
End Class
String class Concat method
Imports System
Public Class MainClass
Shared Sub Main()
Dim string1 As String = "Happy "
Dim string2 As String = "Birthday"
Console.WriteLine("string1 = """ & string1 & """" & _
vbCrLf & "string2 = """ & string2 & """")
Console.WriteLine("Result of String.Concat(string1, string2) = " & _
String.Concat(string1, string2))
End Sub " Main
End Class
String compare
Imports System
Public Class MainClass
Shared Sub Main()
"Declare variable
Dim strName As String
"Get the name
strName = "STEPHANIE"
"Compare the name
If String.rupare(strName, "STEPHANIE", True) = 0 Then
System.Console.WriteLine("Hello, Stephanie!")
End If
End Sub
End Class
String Constructor
Imports System
Public Class MainClass
Shared Sub Main()
Dim characterArray As Char()
Dim quotes As Char = ChrW(34)
Dim originalString, string1, string2, string3, _
string4 As String
characterArray = New Char() {"w"c, "w"c, "w"c, _
"."c, "j"c, "a"c, "v"c, "a"c, "2"c,"s"c,"."c,"o"c,"m"c}
" string initialization
originalString = "www.vbex.ru"
string1 = originalString
string2 = New String(characterArray)
string3 = New String(characterArray, 6, 3)
string4 = New String("C"c, 5)
Console.WriteLine( "string1 = " & quotes & string1 & quotes & _
vbCrLf & "string2 = " & quotes & string2 & quotes & _
vbCrLf & "string3 = " & quotes & string3 & quotes & _
vbCrLf & "string4 = " & quotes & string4 & quotes )
End Sub " Main
End Class
String Copy
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
" the String copy method
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
" use the overloaded operator
Dim s6 As String = s5
Console.WriteLine("s6 = s5: {0}", s6)
End Sub
End Class
String ends with
Imports System
Public Class MainClass
Shared Sub Main()
Dim river As String = " www.vbex.ru "
Console.WriteLine(river.EndsWith("I"))
Console.WriteLine(river.EndsWith("i"))
End Sub
End Class
String.Equals Demo
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
" the String copy method
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
" copy with the overloaded operator
Dim s6 As String = s5
Console.WriteLine("s6 = s5: {0}", s6)
" member method
Console.WriteLine("Does s6.Equals(s5)?: {0}", s6.Equals(s5))
" shared method
Console.WriteLine("Does Equals(s6,s5)?: {0}", _
String.Equals(s6, s5))
End Sub
End Class
String IndexOf and Insert
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
Dim s3 As String = "S3"
s3 = s3 & "Tail"
Dim s5 As String = String.Copy(s2) "
Console.WriteLine("s5 copied from s2: {0}", s5)
Console.WriteLine("String s3 is {0} characters long. ", _
s5.Length)
Console.WriteLine( )
Console.WriteLine("s3: {0}", s3)
Console.WriteLine("s3: ends with 3?: {0}", s3.EndsWith("3"))
Console.WriteLine( )
Console.Write("The first occurrence of 3 ")
Console.WriteLine("in s3 is {0}", s3.IndexOf("3"))
Dim location As Integer = s3.IndexOf("3")
Dim s10 As String = s3.Insert(location, "3")
Console.WriteLine("s10: {0}", s10)
Console.WriteLine( )
End Sub
End Class
String IndexOf and Insert at a position
Imports System
Public Class MainClass
Shared Sub Main()
Dim river As String = " www.vbex.ru "
Console.WriteLine(river.IndexOf("s"))
Console.WriteLine(river.Insert(9, " domain"))
End Sub
End Class
String Insert
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
Dim s3 As String = "Liberty Associates, Inc. provides "
s3 = s3 & "custom .NET development"
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
Dim location As Integer = s3.IndexOf("provides")
Dim s10 As String = s3.Insert(location, "usually ")
Console.WriteLine("s10: {0}", s10)
End Sub
End Class
String InStr
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim A As String = "ABCD"
Console.WriteLine(InStr(A, "C"))
End Sub
End Class
String Object: =, Is and Mid
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim A As String
Dim B As String
A = "Hello"
B = "Hel" + "lo"
console.WriteLine(A = B)
console.WriteLine(A Is B)
Mid(A, 1, 1) = "X"
Mid(B, 1, 1) = "X"
console.WriteLine(A = B)
console.WriteLine(A Is B)
console.WriteLine(A)
End Sub
End Class
String PadLeft
Public Class MainClass
Public Shared Sub Main()
"Strings
Dim strConstant As String = "ABC"
Dim strRepeat As New String("A"c, 20)
Dim strMyString As String = "Hello World"
Console.WriteLine(strMyString.PadLeft(30))
Console.WriteLine(strMyString.PadLeft(20, "."c))
End Sub
End Class
String properties Length and Chars and method CopyTo of class String
Imports System
Public Class MainClass
Shared Sub Main()
Dim string1 As String
Dim characterArray As Char()
Dim i As Integer
Dim quotes As Char = ChrW(34)
string1 = "hello there"
characterArray = New Char(5) {}
Console.WriteLine( "string1: " & quotes & string1 )
Console.WriteLine( " test Length property ")
Console.WriteLine( "Length of string1: " & string1.Length )
Console.WriteLine( " loop through characters in string1 and display reversed ")
Console.WriteLine( "The string reversed is: ")
For i = string1.Length - 1 To 0 Step -1
Console.WriteLine( string1.Chars(i) )
Next
Console.WriteLine( " copy characters from string1 into characterArray ")
string1.CopyTo(0, characterArray, 0, 5)
Console.WriteLine( "The character array is: " )
For i = 0 To characterArray.GetUpperBound(0)
Console.WriteLine( characterArray(i) )
Next
End Sub " Main
End Class
String Replace Demo
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim myString As String
myString = "AAA"
" replace the string...
Dim newString As String
newString = myString.Replace("A", "NNN")
" display the new string...
Console.WriteLine(newString)
End Sub
End Class
String: set value and display
Imports System
Public Class MainClass
Shared Sub Main()
"Declare variable
Dim strData As String
"Set the string value
strData = "Hello, world!"
"Display the results
System.Console.WriteLine(strData)
End Sub
End Class
String Split
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim s1 As String = "One,Two,Three "
Const Space As Char = " "c
Const Comma As Char = ","c
Dim delimiters( ) As Char = {Space, Comma}
Dim output As String = ""
Dim ctr As Integer = 0
Dim resultArray As String( ) = s1.Split(delimiters)
Dim subString As String
For Each subString In resultArray
ctr = ctr + 1
output &= ctr.ToString( )
output &= ": "
output &= subString
output &= Environment.NewLine
Next subString
Console.WriteLine(output)
End Sub
End Class
String: Substring, LastIndexOf
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim s1 As String = "One Two Three Four"
Dim index As Integer
index = s1.LastIndexOf(" ")
Dim s2 As String = s1.Substring((index + 1))
s1 = s1.Substring(0, index)
index = s1.LastIndexOf(" ")
Dim s3 As String = s1.Substring((index + 1))
s1 = s1.Substring(0, index)
index = s1.LastIndexOf(" ")
Dim s4 As String = s1.Substring((index + 1))
s1 = s1.Substring(0, index)
index = s1.LastIndexOf(" ")
Dim s5 As String = s1.Substring((index + 1))
Console.WriteLine("s1: {0}", s1)
Console.WriteLine("s2: {0}", s2)
Console.WriteLine("s3: {0}", s3)
Console.WriteLine("s4: {0}", s4)
Console.WriteLine("s5: {0}", s5)
End Sub
End Class
String Substring method
Imports System
Public Class MainClass
Shared Sub Main()
Dim letters As String = "abcdefghijklmabcdefghijklm"
Dim quotes As Char = ChrW(34)
Console.WriteLine("invoke SubString method and pass it one parameter")
Console.WriteLine("Substring from index 20 to end is " & _
quotes & letters.Substring(20) & quotes )
Console.WriteLine("invoke SubString method and pass it two parameters")
Console.WriteLine("Substring from index 0 to 6 is " & _
quotes & letters.Substring(0, 6) & quotes)
End Sub " Main
End Class
String ToCharArray method
Imports System
Public Class MainClass
Shared Sub Main()
Dim string1 As String = "hello"
Dim string2 As String = "GOOD BYE "
Dim string3 As String = " spaces "
Dim charArray() As Char = string1.ToCharArray()
Console.WriteLine("string1 = " & string1 & vbCrLf & "string2 = " & _
string2 & vbCrLf & "string3 = " & string3)
Console.WriteLine("string1 as a character " & _
"array = ")
End Sub " Main
End Class
String ToLower and ToUpper
Imports System
Public Class MainClass
Shared Sub Main()
Dim string1 As String = "hello"
Dim string2 As String = "GOOD BYE "
Dim string3 As String = " spaces "
Dim charArray() As Char = string1.ToCharArray()
Console.WriteLine("string1 = " & string1 & vbCrLf & "string2 = " & _
string2 & vbCrLf & "string3 = " & string3)
Console.WriteLine("string1.ToUpper() = " & _
string1.ToUpper() & vbCrLf & "string2.ToLOwer() = " & _
string2.ToLower())
End Sub " Main
End Class
String ToString method
Imports System
Public Class MainClass
Shared Sub Main()
Dim string1 As String = "hello"
Dim string2 As String = "GOOD BYE "
Dim string3 As String = " spaces "
Dim charArray() As Char = string1.ToCharArray()
Console.WriteLine("string1 = " & string1 & vbCrLf & "string2 = " & _
string2 & vbCrLf & "string3 = " & string3)
Console.WriteLine("string1 = " & string1.ToString())
End Sub " Main
End Class
String Trim method
Imports System
Public Class MainClass
Shared Sub Main()
Dim string1 As String = "hello"
Dim string2 As String = "GOOD BYE "
Dim string3 As String = " spaces "
Dim charArray() As Char = string1.ToCharArray()
Console.WriteLine("string1 = " & string1 & vbCrLf & "string2 = " & _
string2 & vbCrLf & "string3 = " & string3)
Console.WriteLine("string3 after trim = "" & _
string3.Trim() & """)
End Sub " Main
End Class
Sub string with only one parameter
Public Class MainClass
Public Shared Sub Main()
"Strings
Dim strConstant As String = "ABC"
Dim strRepeat As New String("A"c, 20)
Dim strMyString As String = "Hello World"
Console.WriteLine(strMyString.Substring(0, 5))
Console.WriteLine(strMyString.Substring(6))
End Sub
End Class
The length of the string
Imports System
Public Class MainClass
Shared Sub Main()
System.Console.WriteLine("The length of the string" & _
"The length of the string".Length)
End Sub
End Class
Trim String space
Imports System
Public Class MainClass
Shared Sub Main()
Dim river As String = " www.vbex.ru "
Console.WriteLine(river.Trim())
End Sub
End Class
Two ways to concatenate strings
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
" concatenation method
Dim s3 As String = String.Concat(s1, s2)
Console.WriteLine("s3 concatenated from s1 and s2: {0}", s3)
" use the overloaded operator
Dim s4 As String = s1 & s2
Console.WriteLine("s4 concatenated from s1 & s2: {0}", s4)
End Sub
End Class
Two ways to copy strings
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
" the String copy method
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
" use the overloaded operator
Dim s6 As String = s5
Console.WriteLine("s6 = s5: {0}", s6)
End Sub
End Class
Use String.Equals methods
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
" the String copy method
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
" copy with the overloaded operator
Dim s6 As String = s5
Console.WriteLine("s6 = s5: {0}", s6)
" member method
Console.WriteLine("Does s6.Equals(s5)?: {0}", s6.Equals(s5))
" shared method
Console.WriteLine("Does Equals(s6,s5)?: {0}", _
String.Equals(s6, s5))
End Sub
End Class
Use the Mid function to replace within the string
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim s1 As String = "abcd"
Dim s2 As String = "ABCD"
Dim s3 As String = "Liberty Associates, Inc. provides "
s3 = s3 & "custom .NET development"
Dim s5 As String = String.Copy(s2)
Console.WriteLine("s5 copied from s2: {0}", s5)
Dim location As Integer = s3.IndexOf("provides")
" you can combine the two as follows:
Dim s11 As String = _
s3.Insert(s3.IndexOf("provides"), "usually ")
"use the Mid function to replace within the string
Mid(s11, s11.IndexOf("usually") + 1, 9) = "always!"
Console.WriteLine("s11 now: {0}", s11)
End Sub
End Class
Using String searching methods: IndexOf
Imports System
Public Class MainClass
Shared Sub Main()
Dim letters As String = "abcdefghijklmabcdefghijklm"
Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
Console.WriteLine(" test IndexOf to locate a character in a string")
Console.WriteLine("""c"" is located at index " & _
letters.IndexOf("c"c) )
Console.WriteLine("""a"" is located at index " & _
letters.IndexOf("a"c, 1) )
Console.WriteLine("""$"" is located at index " & _
letters.IndexOf("$"c, 3, 5) )
End Sub " Main
End Class