VB.Net IndexOf的6种使用方法
来源:百度文库 编辑:神马文学网 时间:2024/05/01 11:28:58
1. Using IndexOf
First, here we see how you can locate the first index of a String in a source String. The example has an input String, and we search for the location of "Vader" with the IndexOf function. If the string is not found, the result is -1.
--- Program that uses IndexOf (VB) --- Module Module1 Sub Main() ' The input string you are using Dim s As String = "Darth Vader has pulmonary problems." ' Does the string contain "Vader"? If (s.IndexOf("Vader") <> -1) Then Console.Write("string contains 'Vader'") End If ' Finished Console.ReadLine() End Sub End Module --- Output of the program --- string contains 'Vader'
Notes on example above. The string "Vader" is found in the input String, at index 6. The console program then prints the message inside the If statement. The final statement ReadLine is there so you can run the program without it exiting.
2. Using IndexOf in Do While Loop
Here we look at looping with IndexOf on Strings. Sometimes you need to locate the first index of a String, and then continue locating further instances. You can accomplish this with a Do While construct and the IndexOf method.
~~~ Program that uses IndexOf and While (VB) ~~~ Module Module1 Sub Main() ' The input String dim Dim s As String = "You have a cat" ' The interation dim Dim i As Integer = s.IndexOf("a"c) ' Loop over the found indexes Do While (i <> -1) ' Write the substring Console.WriteLine(s.Substring(i)) ' Get next index i = s.IndexOf("a"c, i + 1) Loop Console.ReadLine() End Sub End Module ~~~ Output of the program ~~~ ave a cat a cat at
Description of the example VB.NET code. The example first declares the Dim String, which contains the letter 'a' in three places. Next, we call IndexOf on the String.
Do While construct. The Do While loop continues until IndexOf returns -1. Note that if the letter never occurs, the loop will never be entered. The loop prints out the Substring located at the index found by IndexOf. The three lines printed all start with the letter "a".
3. IndexOf Functions
Here we look at an overview of these methods in the .NET Framework. There are four Functions in the IndexOf family available in VB.NET. Usually you will only need the first, IndexOf, but the others are occasionally valuable.
IndexOf Function. This finds the first index of a Char or String and you can specify start and ending indexes of where you want to search. The IndexOfAny Function finds the first index of any of the characters in the Array you send it.
LastIndexOf Function. This is the same as IndexOf but starts searching from the end. The LastIndexOfAny Function finds the last index of any of the characters in the Array it receives.
[NextPage]
4. Using IndexOf with Substring
Here we see a simple example of using the Substring Function in VB.NET with the result of IndexOf. The IndexOf call below locates the first index of the uppercase letter 'B'.
=== Program that uses Substring (VB) === Module Module1 Sub Main() 'vb.net源码和实例,来自乐博网 www.lob.cn ' The string you are searching Dim s As String = "Visual Basic rocks" ' Find index of uppercase letter 'B' Dim i As Integer = s.IndexOf("B"c) ' This new string contains the substring starting at B Dim part As String = s.Substring(i) Console.WriteLine(part) Console.ReadLine() End Sub End Module === Output of the program === Basic rocks
5. Using IndexOfAny
The next example here shows how you can use the IndexOfAny Function. This Function is useful when you have a set of characters and you are searching for any of them. Internally, the IndexOfAny Function simply uses an array Loop.
--- Program that uses IndexOfAny (VB) --- Module Module1 Sub Main() ' The input String Dim s1 As String = "Darth is not my father." ' Find the first index of either "x" or "n" Dim i1 As Integer = s1.IndexOfAny(New Char() {"x"c, "n"c}) If (i1 <> -1) Then Console.WriteLine(s1.Substring(i1)) End If ' Another input String Dim s2 As String = "C# is challenging." ' Find the first index of either '#' or ' ' Dim i2 As Integer = s2.IndexOfAny(New Char() {"#"c, " "c}) If (i2 <> -1) Then Console.WriteLine(s2.Substring(i2)) End If Console.ReadLine() End Sub End Module --- Output of the program --- not my father. # is challenging.
Important note. The result value from IndexOfAny is checked for -1 each time it is called. Make sure you always check for -1, because many times using -1 elsewhere in your application will cause an error. It is invalid.
6. Contains Function
In the .NET platform, the Contains Function on System.String simply calls IndexOf internally. Therefore, you should only use it if you want a different value to indicate failure—False instead of -1.
VB.NET Function: Contains Result when substring exists: True Result when substring does NOT exist: False VB.NET Function: IndexOf Result when substring exists: >= 0 Result when substring does NOT exist: -1
7. Performance tip for IndexOf Function
The most important performance tip the author knows is to use a character instead of a one-character String. In some cases, the character overload is 85% faster. It will return an equivalent value. [C# IndexOf String Examples - dotnetperls.com]
8. Summary
Here we covered several examples of using the IndexOf Function in VB.NET. We saw how to use IndexOf with Strings and characters; how to use IndexOf in Do While loops; how to use IndexOfAny; and how to check the return value of IndexOf. This is a powerful Function in Visual Basic .NET and can simplify your program. Use it instead of duplicating For loops.