如何从字符串中提取2个字符

时间:2014-07-09 08:59:43

标签: vb.net vb6

根据以下算法

混淆了从给定字符串中提取子字符串

算法

每个输入字符串的格式为:3nm,4gn,77jk等...(即,一个字符串后跟一个数字) 现在我需要的是从字符串中提取字母..

示例

输入:77nm 输出:nm

我的贡献:

Private Function getbyte(s As String, ByVal place As Integer) As String
   If place < Len(s) Then
      place = place + 1
      getbyte = Mid(s, place, 1)
   Else
      getbyte = ""
   End If
End Function

但它不能解决我的问题,因为它返回数字和数字

1 个答案:

答案 0 :(得分:2)

您可以使用LINQ仅提取字母:

Dim input As String = "3nm , 4gn , 77jk"
Dim output As String = New String((From c As Char In input Select c Where Char.IsLetter(c)).ToArray())

结果:

  

nmgnjk

您还可以将LINQ封装到扩展方法中。

<Extension()>
Public Module Extensions

    <Extension()>
    Public Function Extract(input As String, predicate As Predicate(Of Char)) As String
        Return New String((From c As Char In input Select c Where predicate.Invoke(c)).ToArray())
    End Function

End Module

用法

output = input.Extract(Function(c As Char) Char.IsLetter(c))