Module Module1 Sub Main() ' 定義3個字符串變量 Dim str1, str2, str3 As String '給str1,str2付初值 str1 = "Hello" : str2 = "World" Console.WriteLine("方法Concat") '連接一個或多個字符串 Console.WriteLine("str1={0},str2={1},String.Concat(str1,str2)={2}", str1, str2, String.Concat(str1, str2)) '判斷字符串中是否具有相同的值返回類型為布爾型 Console.WriteLine("方法Equals") Console.WriteLine("str1={0},str2={1},String.Equals(str2)={2}", str1, str2, str1.Equals(str2)) '在字符串中指定索引位置插入指定的字符串 Console.WriteLine("方法Insert") str1 = "0123456789" : str2 = "aaa" Console.WriteLine("str1={0},str2={1},str1.Insert(5,str2)={2}", str1, str2, str1.Insert(5, str2)) '左右對齊字符串中的字符 Console.WriteLine("方法PadLeft/PadRight") str1 = "World" : str2 = "世界" Console.WriteLine("str1={0}", str1) Console.WriteLine("str1.PadLeft(8,""*"")={0},str.PadRight(8,""*"")={1}", str1.PadLeft(8, "*"), str1.PadRight(8, "*")) Console.WriteLine("str1={0}", str2) Console.WriteLine("str2.PadLeft(8,""*"")={0},str2.PadRight(8,""*"")={1}", str2.PadLeft(8, "*"), str2.PadRight(8, "*")) '找出指定字符串或字符轉在此字符串中的第一個、最后一個匹配項的索引位置 Console.WriteLine("方法IndexOf/LastIndexOf") str1 = "Visual Basic.NET,ASP.NET,C#.NET" Console.WriteLine("str1={0}", str1) Console.WriteLine("str1.IndexOf(""NET"")={0},str1.LastIndexOf(""NET"")={1}", str1.IndexOf("NET"), str1.LastIndexOf("NET")) '將字符串中的字符復制到字符串數組 Console.WriteLine("方法ToCharArray") str2 = str1.ToCharArray(str1.IndexOf("NET"), 3) Console.WriteLine("str1={0}", str1) Console.WriteLine("str1.ToCharArray(str1.IndexOf(""NET""),3)={0}", str2) '在指定字符串數組的每個元素之間串聯指定的分隔符,產生單個串聯的字符串 Console.WriteLine("方法Join") Dim myArray(3) As String myArray(0) = "I" : myArray(1) = "am" : myArray(2) = "a" : myArray(3) = "student" For i As Integer = 0 To 3 Console.Write("myArrat({0})={1}", i, myArray(i)) Next Console.WriteLine() Console.WriteLine("String.Join(""*"",myArray)={0}", String.Join("*", myArray)) '此字符串中刪除指定個數字符 Console.WriteLine("方法Remove") str1 = "0123456789" Console.WriteLine("str1={0},str1.Remove(2,3)={1}", str1, str1.Remove(2, 3)) '將此字符串指定字符串字符的所有匹配項代替為其他指定字符串 Console.WriteLine("方法Replace") str2 = "123" str3 = "abc" Console.WriteLine("str1={0}, str2={1}, str3={2}, str1.Replace(str2, str3)={3}", str1, str2, str3, str1.Replace(str2, str3)) '從此字符串檢索字符串 Console.WriteLine("方法SubString") Console.WriteLine("str1={0},str1.Substring(3,4)={1}", str1, str1.Substring(3, 4)) Console.ReadLine() End Sub End Module