我试图获取字符串的最后一个字。我用空格分割字符串,然后得到Last,但它显示错误" Last不是String()"
的成员str = "This is a string of words"
str = currentPriceString.Split(" ").Last
我做错了什么?
注意:我有一个System.Linq参考
答案 0 :(得分:0)
您提到了程序集引用,但似乎您错过了在包含您显示的代码单元的类/模块顶部指定导入。
Imports System.Linq
如果您还有导入,那么可能会发生的情况是您的目标是.NetFx版本低于v3.5,其中不存在 LINQ 功能,请检查您的.Net Framework目标并将其增加到v3.5或更高。
答案 1 :(得分:0)
Dim str As String = "This is a string of words"
Dim getLastWord As String = Mid(str, str.LastIndexOf(" ") + 2)
这将使任何字符超过String中的最后{space}。
答案 2 :(得分:0)
为了使用IEnumerable.Last(Of TSource)
扩展方法,您的项目必须以.NET 3.5或更高版本为目标。
请参阅下面的代码示例:
Option Strict On
Option Explicit On
' The imports below are only required if the namespace(s)
' have not been imported into your project's settings
Imports System.Linq ' Requires .NET 3.5 or higher
Imports Microsoft.VisualBasic ' This is required to use the Visual Basic functions like Mid(), InStr(), MsgBox(), etc...
Module Module1
Sub Main()
Dim str As String = "This is a string of words"
Dim delim As Char = " "c
Dim strArr As String() = str.Split(delim)
Dim lastWord As String
' Requires the Microsoft Visual Basic namespace to be imported
lastWord = Mid(str, InStrRev(str, delim) + 1I)
lastWord = Mid(str, str.LastIndexOf(delim) + 2I)
' These are safe for .NET 2.0 and higher:
lastWord = strArr(strArr.Length - 1I)
lastWord = strArr(strArr.GetUpperBound(0I))
' These require .NET 3.5 and higher:
lastWord = (From word As String In strArr Select word).Last
lastWord = strArr.Last
lastWord = strArr.ElementAt(strArr.GetUpperBound(0I))
End Sub
End Module