Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# alternative to VBScript InStrRev function

Posted on 2004-10-13
3
Medium Priority
?
2,892 Views
Last Modified: 2012-08-13
I need to search a string for multiple occurences of numbers separated by a hyphen (-).  A typical example would be the string "seahorse_se-120-181-3794-pr", where I need the last two sets of digits - the 181 and the 3794.  These numbes are not a fixed number of digits - they can be from 1 to 4 digits in length; and because nothing is ever easy, the string in front of the numbers (the "seahorse_se-120-" in the example above) can be any length also.  I am using VBScript in an ASP page right now, and the InStrRev function works fine to help me extract what I need.  Is there a C# equivalent?

Thanks for your help,

Rod
0
Comment
Question by:okanagan
  • 2
3 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 12303190
My C# isnt the best, but i have a VBA version of it, tried to convert it the VB .Net (dont have it at hand so cant test)

Dave

Public Function InStrRev(StringCheck As String, StringMatch As String, Optional Start As Long = -1)

    Dim sTemp As String

    If Start <= 0 Then
        sTemp = StringCheck
    Else
        sTemp = Left(StringCheck, Start)
    End If
   
    Do Until sTemp = ""
        If Mid(StringCheck, Len(sTemp), Len(StringMatch)) = StringMatch Then
            Exit Do
        End If
        sTemp = Left(sTemp, Len(sTemp) - 1)
    Loop
    If sTemp = "" Then
        return 0
    Else
        return Len(sTemp)
    End If

End Function
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 1000 total points
ID: 12303745
What you are looking for is string.LastIndexOf() - it's a .NET framework thing, not a C# specific thing.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12303755
(But for your task, I'd use a regular expression anyways.)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question