Solved

C# alternative to VBScript InStrRev function

Posted on 2004-10-13
3
2,692 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 250 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now