本文整理汇总了VB.NET中System.String.LastIndexOf方法的典型用法代码示例。如果您正苦于以下问题:VB.NET String.LastIndexOf方法的具体用法?VB.NET String.LastIndexOf怎么用?VB.NET String.LastIndexOf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.LastIndexOf方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Sample
' This code example demonstrates the
' System.String.LastIndexOf(String, ..., StringComparison) methods.
Imports System.Threading
Imports System.Globalization
Class Sample
Public Shared Sub Main()
Dim intro As String = "Find the last occurrence of a character using different " & _
"values of StringComparison."
Dim resultFmt As String = "Comparison: {0,-28} Location: {1,3}"
' Define a string to search for.
' U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
Dim CapitalAWithRing As String = "Å"
' Define a string to search.
' The result of combining the characters LATIN SMALL LETTER A and COMBINING
' RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character
' LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
Dim cat As String = "A Cheshire c" & "å" & "t"
Dim loc As Integer = 0
Dim scValues As StringComparison() = { _
StringComparison.CurrentCulture, _
StringComparison.CurrentCultureIgnoreCase, _
StringComparison.InvariantCulture, _
StringComparison.InvariantCultureIgnoreCase, _
StringComparison.Ordinal, _
StringComparison.OrdinalIgnoreCase }
Dim sc As StringComparison
' Clear the screen and display an introduction.
Console.Clear()
Console.WriteLine(intro)
' Display the current culture because culture affects the result. For example,
' try this code example with the "sv-SE" (Swedish-Sweden) culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
Console.WriteLine("The current culture is ""{0}"" - {1}.", _
Thread.CurrentThread.CurrentCulture.Name, _
Thread.CurrentThread.CurrentCulture.DisplayName)
' Display the string to search for and the string to search.
Console.WriteLine("Search for the string ""{0}"" in the string ""{1}""", _
CapitalAWithRing, cat)
Console.WriteLine()
' Note that in each of the following searches, we look for
' LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains
' LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates
' the string was not found.
' Search using different values of StringComparsion. Specify the start
' index and count.
Console.WriteLine("Part 1: Start index and count are specified.")
For Each sc In scValues
loc = cat.LastIndexOf(CapitalAWithRing, cat.Length - 1, cat.Length, sc)
Console.WriteLine(resultFmt, sc, loc)
Next sc
' Search using different values of StringComparsion. Specify the
' start index.
Console.WriteLine(vbCrLf & "Part 2: Start index is specified.")
For Each sc In scValues
loc = cat.LastIndexOf(CapitalAWithRing, cat.Length - 1, sc)
Console.WriteLine(resultFmt, sc, loc)
Next sc
' Search using different values of StringComparsion.
Console.WriteLine(vbCrLf & "Part 3: Neither start index nor count is specified.")
For Each sc In scValues
loc = cat.LastIndexOf(CapitalAWithRing, sc)
Console.WriteLine(resultFmt, sc, loc)
Next sc
End Sub
End Class
'
'Note: This code example was executed on a console whose user interface
'culture is "en-US" (English-United States).
输出:
Find the last occurrence of a character using different values of StringComparison. The current culture is "en-US" - English (United States). Search for the string "Å" in the string "A Cheshire ca°t" Part 1: Start index and count are specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 2: Start index is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 3: Neither start index nor count is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1
示例2: Example
Public Module Example
Public Sub Main()
Dim searchString As String = ChrW(&h00AD) + "m"
Dim s1 As String = "ani" + ChrW(&h00AD) + "mal"
Dim s2 As String = "animal"
Dim position As Integer
position = s1.LastIndexOf("m"c)
If position >= 0 Then
Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.CurrentCulture))
Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.Ordinal))
End If
position = s2.LastIndexOf("m"c)
If position >= 0 Then
Console.WriteLine(s2.LastIndexOf(searchString, position, StringComparison.CurrentCulture))
Console.WriteLine(s2.LastIndexOf(searchString, position, StringComparison.Ordinal))
End If
End Sub
End Module
输出:
4 3 3 -1
示例3: Sample
' Sample for String.LastIndexOf(String, Int32, Int32)
_
Class Sample
Public Shared Sub Main()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim count As Integer
Dim [end] As Integer
start = str.Length - 1
[end] = start / 2 - 1
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, [end])
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("The string 'he' occurs at position(s): ")
count = 0
at = 0
While start > - 1 And at > - 1
count = start - [end] 'Count must be within the substring.
at = str.LastIndexOf("he", start, count)
If at > - 1 Then
Console.Write("{0} ", at)
start = at - 1
End If
End While
Console.Write("{0}{0}{0}", Environment.NewLine)
End Sub
End Class
输出:
All occurrences of 'he' from position 66 to 32. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 56 45
示例4: Example
Module Example
Public Sub Main()
Dim position As Integer
Dim softHyphen As String = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the soft hyphen.
position = s1.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s1.LastIndexOf(softHyphen, position, position + 1))
End If
position = s2.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s2.LastIndexOf(softHyphen, position, position + 1))
End If
' Find the index of the soft hyphen followed by "n".
position = s1.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s1.LastIndexOf(softHyphen + "n", position, position + 1))
End If
position = s2.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s2.LastIndexOf(softHyphen + "n", position, position + 1))
End If
' Find the index of the soft hyphen followed by "m".
position = s1.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s1.LastIndexOf(softHyphen + "m", position, position + 1))
End If
position = s2.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s2.LastIndexOf(softHyphen + "m", position, position + 1))
End If
End Sub
End Module
输出:
m' at position 4 4 m' at position 3 3 m' at position 4 1 m' at position 3 1 m' at position 4 4 m' at position 3 3
示例5: Sample
' Sample for String.LastIndexOf(Char, Int32, Int32)
_
Class Sample
Public Shared Sub Main()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim count As Integer
Dim [end] As Integer
start = str.Length - 1
[end] = start / 2 - 1
Console.WriteLine("All occurrences of 't' from position {0} to {1}.", start, [end])
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("The letter 't' occurs at position(s): ")
count = 0
at = 0
While start > - 1 And at > - 1
count = start - [end] 'Count must be within the substring.
at = str.LastIndexOf("t"c, start, count)
If at > - 1 Then
Console.Write("{0} ", at)
start = at - 1
End If
End While
Console.Write("{0}{0}{0}", Environment.NewLine)
End Sub
End Class
输出:
All occurrences of 't' from position 66 to 32. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 64 55 44 41 33
示例6: Example
Module Example
Public Sub Main()
Dim softHyphen As String = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
Console.WriteLine("Culture-sensitive comparison:")
' Use culture-sensitive comparison to find the last soft hyphen.
Console.WriteLine(s1.LastIndexOf(softHyphen, StringComparison.CurrentCulture))
Console.WriteLine(s2.LastIndexOf(softHyphen, StringComparison.CurrentCulture))
' Use culture-sensitive comparison to find the last soft hyphen followed by "n".
Console.WriteLine(s1.LastIndexOf(softHyphen + "n", StringComparison.CurrentCulture))
Console.WriteLine(s2.LastIndexOf(softHyphen + "n", StringComparison.CurrentCulture))
' Use culture-sensitive comparison to find the last soft hyphen followed by "m".
Console.WriteLine(s1.LastIndexOf(softHyphen + "m", StringComparison.CurrentCulture))
Console.WriteLine(s2.LastIndexOf(softHyphen + "m", StringComparison.CurrentCulture))
Console.WriteLine("Ordinal comparison:")
' Use ordinal comparison to find the last soft hyphen.
Console.WriteLine(s1.LastIndexOf(softHyphen, StringComparison.Ordinal))
Console.WriteLine(s2.LastIndexOf(softHyphen, StringComparison.Ordinal))
' Use ordinal comparison to find the last soft hyphen followed by "n".
Console.WriteLine(s1.LastIndexOf(softHyphen + "n", StringComparison.Ordinal))
Console.WriteLine(s2.LastIndexOf(softHyphen + "n", StringComparison.Ordinal))
' Use ordinal comparison to find the last soft hyphen followed by "m".
Console.WriteLine(s1.LastIndexOf(softHyphen + "m", StringComparison.Ordinal))
Console.WriteLine(s2.LastIndexOf(softHyphen + "m", StringComparison.Ordinal))
End Sub
End Module
输出:
6 5 1 1 4 3 Ordinal comparison: 3 -1 -1 -1 3 -1
示例7: Test
' 导入命名空间
Imports System.IO
Public Module Test
Public Sub Main()
Dim filename As String
filename = ExtractFilename("C:\temp\")
Console.WriteLine("{0}", IIf(String.IsNullOrEmpty(fileName), "<none>", filename))
filename = ExtractFilename("C:\temp\delegate.txt")
Console.WriteLine("{0}", IIf(String.IsNullOrEmpty(fileName), "<none>", filename))
filename = ExtractFilename("delegate.txt")
Console.WriteLine("{0}", IIf(String.IsNullOrEmpty(fileName), "<none>", filename))
filename = ExtractFilename("C:\temp\notafile.txt")
Console.WriteLine("{0}", IIf(String.IsNullOrEmpty(fileName), "<none>", filename))
End Sub
Public Function ExtractFilename(filepath As String) As String
' If path ends with a "\", it's a path only so return String.Empty.
If filepath.Trim().EndsWith("\") Then Return String.Empty
' Determine where last backslash is.
Dim position As Integer = filepath.LastIndexOf("\"c)
' If there is no backslash, assume that this is a filename.
If position = -1 Then
' Determine whether file exists in the current directory.
If File.Exists(Environment.CurrentDirectory + Path.DirectorySeparatorChar + filepath) Then
Return filepath
Else
Return String.Empty
End If
Else
' Determine whether file exists using filepath.
If File.Exists(filepath) Then
' Return filename without file path.
Return filepath.Substring(position + 1)
Else
Return String.Empty
End If
End If
End Function
End Module
输出:
delegate.txt
示例8: Sample
' Sample for String.LastIndexOf(Char, Int32)
Imports System
_
Class Sample
Public Shared Sub Main()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
start = str.Length - 1
Console.WriteLine("All occurrences of 't' from position {0} to 0.", start)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("The letter 't' occurs at position(s): ")
at = 0
While start > - 1 And at > - 1
at = str.LastIndexOf("t"c, start)
If at > - 1 Then
Console.Write("{0} ", at)
start = at - 1
End If
End While
Console.Write("{0}{0}{0}", Environment.NewLine)
End Sub
End Class
输出:
All occurrences of 't' from position 66 to 0. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 64 55 44 41 33 11 7
示例9: Example
Module Example
Public Sub Main()
Dim strSource As String() = { "<b>This is bold text</b>", _
"<H1>This is large Text</H1>", _
"<b><i><font color=green>This has multiple tags</font></i></b>", _
"<b>This has <i>embedded</i> tags.</b>", _
"This line ends with a greater than symbol and should not be modified>" }
' Strip HTML start and end tags from each string if they are present.
For Each s As String In strSource
Console.WriteLine("Before: " + s)
' Use EndsWith to find a tag at the end of the line.
If s.Trim().EndsWith(">") Then
' Locate the opening tag.
Dim endTagStartPosition As Integer = s.LastIndexOf("</")
' Remove the identified section if it is valid.
If endTagStartPosition >= 0 Then
s = s.Substring(0, endTagStartPosition)
End If
' Use StartsWith to find the opening tag.
If s.Trim().StartsWith("<") Then
' Locate the end of opening tab.
Dim openTagEndPosition As Integer = s.IndexOf(">")
' Remove the identified section if it is valid.
If openTagEndPosition >= 0 Then
s = s.Substring(openTagEndPosition + 1)
End If
End If
End If
' Display the trimmed string.
Console.WriteLine("After: " + s)
Console.WriteLine()
Next
End Sub
End Module
输出:
Before: This is bold text After: This is bold text Before:This is large Text
After: This is large Text Before: This has multiple tags After: This has multiple tags Before: This has embedded tags. After: This has embedded tags. Before: This line ends with a greater than symbol and should not be modified> After: This line ends with a greater than symbol and should not be modified>
示例10: Example
Module Example
Public Sub Main()
Dim softHyphen As String = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the last soft hyphen.
Console.WriteLine(s1.LastIndexOf(softHyphen))
Console.WriteLine(s2.LastIndexOf(softHyphen))
' Find the index of the last soft hyphen followed by "n".
Console.WriteLine(s1.LastIndexOf(softHyphen + "n"))
Console.WriteLine(s2.LastIndexOf(softHyphen + "n"))
' Find the index of the last soft hyphen followed by "m".
Console.WriteLine(s1.LastIndexOf(softHyphen + "m"))
Console.WriteLine(s2.LastIndexOf(softHyphen + "m"))
End Sub
End Module
输出:
6 5 1 1 4 3
示例11: Example
Public Module Example
Public Sub Main()
Dim searchString As String = ChrW(&h00AD) + "m"
Dim s1 As String = "ani" + ChrW(&h00AD) + "m"
Dim s2 As String = "animal"
Dim position As Integer
position = s1.LastIndexOf("m"c)
If position >= 1 Then
Console.WriteLine(s1.LastIndexOf(searchString, position, position, StringComparison.CurrentCulture))
Console.WriteLine(s1.LastIndexOf(searchString, position, position, StringComparison.Ordinal))
End If
position = s2.LastIndexOf("m"c)
If position >= 1 Then
Console.WriteLine(s2.LastIndexOf(searchString, position, position, StringComparison.CurrentCulture))
Console.WriteLine(s2.LastIndexOf(searchString, position, position, StringComparison.Ordinal))
End If
End Sub
End Module
输出:
4 3 3 -1
示例12: Sample
' Sample for String.LastIndexOf(String, Int32)
_
Class Sample
Public Shared Sub Main()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
'#3
start = str.Length - 1
Console.WriteLine("All occurrences of 'he' from position {0} to 0.", start)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("The string 'he' occurs at position(s): ")
at = 0
While start > - 1 And at > - 1
at = str.LastIndexOf("he", start)
If at > - 1 Then
Console.Write("{0} ", at)
start = at - 1
End If
End While
Console.Write("{0}{0}{0}", Environment.NewLine)
End Sub
End Class
输出:
All occurrences of 'he' from position 66 to 0. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 56 45 8
示例13: Example
Module Example
Public Sub Main()
Dim position As Integer
Dim softHyphen As String = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the soft hyphen.
position = s1.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s1.LastIndexOf(softHyphen, position))
End If
position = s2.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s2.LastIndexOf(softHyphen, position))
End If
' Find the index of the soft hyphen followed by "n".
position = s1.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s1.LastIndexOf(softHyphen + "n", position))
End If
position = s2.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s2.LastIndexOf(softHyphen + "n", position))
End If
' Find the index of the soft hyphen followed by "m".
position = s1.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s1.LastIndexOf(softHyphen + "m", position))
End If
position = s2.LastIndexOf("m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(s2.LastIndexOf(softHyphen + "m", position))
End If
End Sub
End Module
输出:
m' at position 4 4 m' at position 3 3 m' at position 4 1 m' at position 3 1 m' at position 4 4 m' at position 3 3
示例14: MainClass
' 导入命名空间
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