VB.Net Tutorial/Data Type/String Find

Материал из VB Эксперт
Перейти к: навигация, поиск

Demonstrating StartsWith and EndsWith methods

Module Tester
   Sub Main()
      Dim strings As String()
      Dim output As String = ""
      Dim i As Integer
      Dim quotes As Char = ChrW(34)
      strings = New String() {"started", "starting", _
         "ended", "ending"}
      For i = 0 To strings.GetUpperBound(0)
         If strings(i).StartsWith("st") Then
            Console.WriteLine(" starts with st")
         End If
      Next
      For i = 0 To strings.GetUpperBound(0)
         If strings(i).EndsWith("ed") Then
            Console.WriteLine(" ends with ed" )
         End If
      Next
   End Sub " Main
End Module
starts with st
 starts with st
 ends with ed
 ends with ed

Return the index of the string

Option Strict On
 Imports System
 Class Tester
     Public Shared Sub Main( )
         Dim s1 As String = "abcd"
         Dim s2 As String = "ABCD"
         Dim s3 As String = "AAAAs "
         s3 = s3 & "development"
         Console.WriteLine("s3: {0}", s3)
         
         Console.Write("The first occurrence of a ")
         Console.WriteLine(s3.IndexOf("a"))

     End Sub "Main
 End Class "Tester
s3: AAAAs development
The first occurrence of a -1

String Index of any

public class Test
   public Shared Sub Main
               Dim s1 As New String("Greeting")
        Console.WriteLine(s1.IndexOfAny("tin"))
   End Sub
End class
4

Use IndexOfAny to find first occurrence of character in array

Module Tester
   Sub Main()
      Dim letters As String = "abcdefghijklmabcdefghijklm"
      Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
      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 
End Module
First occurrence of "c", "a" or "$" is located at 0
First occurrence of "c", "a" or "$" is located at 13
First occurrence of "c", "a" or "$" is located at -1

Use IndexOf to locate a substring in a string

Module Tester
   Sub Main()
      Dim letters As String = "abcdefghijklmabcdefghijklm"
      Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
      
      Console.WriteLine(letters.IndexOf("def"))
      Console.WriteLine(letters.IndexOf("def", 7))
      Console.WriteLine(letters.IndexOf("hello", 5, 15))
   End Sub 
End Module
3
16
-1

Use LastIndexOfAny to find first occurrence of character in array

Module Tester
   Sub Main()
      Dim letters As String = "abcdefghijklmabcdefghijklm"
      Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
      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 
End Module
Last occurrence of "c", "a" or "$" is located at 15
Last occurrence of "c", "a" or "$" is located at 0
Last occurrence of "c", "a" or "$" is located at -1

Use LastIndexOf to find a substring in a string

Module Tester
   Sub Main()
      Dim letters As String = "abcdefghijklmabcdefghijklm"
      Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
      Console.WriteLine(letters.LastIndexOf("def"))
      Console.WriteLine(letters.LastIndexOf("def", 25))
      Console.WriteLine(letters.LastIndexOf("hello", 20, 15))   
   End Sub 
End Module
16
16
-1