Solved

C# alternative to VBScript InStrRev function

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
FTP file download using c# 3 25
Firing an ASP.NET event on selection of a JQuery datepicker 4 30
"Emulate" TAB key when press Enter Key 3 46
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…

839 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