user3818752
user3818752

Reputation:

how to extract 2 characters from a string

am in a confusion of extracting sub string fro a given string according to the following algorithm,

Algorithm

every input string is of the format: 3nm , 4gn , 77jk so on..(ie., a string followed by a number) now what i need is that extract the alphabet from the string..

example

input: 77nm output : nm

my contribution:

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

but it will not solve my problem as it returns number as well as digits

Upvotes: 0

Views: 211

Answers (1)

You can use LINQ to extract only letters:

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())

Result:

nmgnjk

You can also encapsulate the LINQ into an extension method.

<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

Usage

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

Upvotes: 2

Related Questions