简体   繁体   English

Last不是String()的成员-VB.Net获取字符串的最后一个单词

[英]Last is not a member of String() - VB.Net getting last word of string

I'm trying to get the last word of a string. 我正在尝试获取字符串的最后一个单词。 I split the string by spaces, and then get Last, but it's showing an error "Last is not a member of String()" 我用空格分割了字符串,然后得到Last,但是显示错误“ Last不是String()的成员”

str = "This is a string of words"
str = currentPriceString.Split(" ").Last

What am I doing wrong? 我究竟做错了什么?

note: I do have a System.Linq reference 注意:我确实有一个System.Linq参考

You mentioned the assembly reference, but seems that you missed to specify the import at the top of the class/module that contains that unit of code you shown. 您提到了程序集引用,但是似乎您错过了在包含所显示代码单元的类/模块顶部指定导入的操作。

Imports System.Linq

If you also have the import, then probably what happens is that you are targeting a .NetFx version lower than v3.5, where doesn't not exist LINQ features, check your .Net Framework targetting and increase it up to v3.5 or greater. 如果还具有导入功能,则可能会发生以下情况:您将目标定位为低于v3.5的.NetFx版本(在该版本中不存在LINQ功能),请检查您的.Net Framework目标并将其提高到v3.5或更高版本。更大。

Dim str As String = "This is a string of words"
Dim getLastWord As String = Mid(str, str.LastIndexOf(" ") + 2)

This will achieve getting any Characters past the last {space} in the String. 这样可以使所有字符都超过字符串的最后{space}。

In order to use the IEnumerable.Last(Of TSource) extension method, your project must target .NET 3.5 or higher. 为了使用IEnumerable.Last(Of TSource)扩展方法,您的项目必须以.NET 3.5或更高版本为目标。

Please see the code example below: 请参见下面的代码示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM