Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# alternative to VBScript InStrRev function

Posted on 2004-10-13
3
Medium Priority
?
2,861 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

704 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