久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

VB.Net IndexOf的6種使用方法

 nxhujiee 2010-04-15
VB.Net IndexOf的6種使用方法
作者:Ilu    來(lái)源:樂博網(wǎng)整理     更新時(shí)間:2009-9-5

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源碼和實(shí)例,,來(lái)自樂博網(wǎng) 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.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多