tags:

views:

25

answers:

3

i need to get a set of values after certain 14 length of a string and before the last 5 stringss.

eg:

Theboyisgoingt9123holdi: so i need to get the value 9123 iamfullofmeats89holdi: i need to extract value 89

so the algorithm here is, i am trying to extract the values that comes after the 14th length of the string and just before the last 5 characters of the same string. its always whats between 14th and last 5 characters.

i am coding in vb.net. any ideas is great appreciated.

A: 

This is a great place to use a regular expression

Function GetNumber(ByVal str As String) As String
  Dim match = Regex.Match("\d+", str)
  if math.Sucess then
    Return match.Value
  Else
    Return String.Empty
  End If
End Function
JaredPar
thanks, but i forgot to mention, my original strings can be a mixture of strings and numbers: Theboyisgoingt9123holdi or 012boyisgoingt9123holdi or Theboyisgoingt9123ho555 or Theboyisg123gt9123holdi. thanks.
Menew
+2  A: 
Dim ResultString As String
ResultString = Regex.Match(SubjectString, "(?<=^.{14}).*(?=.{5}$)").Value

will give you the characters from the 15th until the 6th-to-last character in the string. It is assumed that there are no newlines in the string. If there are, and you want to treat them just like any other character, use the option RegexOptions.Singleline as an additional parameter to Regex.Match().

Explanation:

(?<=^.{14}): Match a position that is after the 14th character in the string.

.*: Match anything until...

(?=.{5}$): ...the position before the last 5 characters in the string.

Tim Pietzcker
this is exactly what i was looking for. many thanks!!!
Menew
+1  A: 

I too would go with a regex. This is how I would do it:

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim str As String = "Theboyisgoingt9123holdi"        
        Dim m As Match = Regex.Match(str, "^.{14}(.+).{5}$")
        If m.Success Then
            Console.WriteLine(m.Groups(1).Value)
        End If
    End Sub

End Module
steinar