Solved

Find last occurance of a substring within a string

Posted on 2013-01-03
10
423 Views
Last Modified: 2013-01-08
I have the following string: which begins after the last '//':

"abc//def//hij//I am the owner"

I need to return "I am the owner" which always begins after the last '//':

The length of the final substring can vary.

What is the best way to do this?

Thanks
0
Comment
Question by:Dovberman
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 29

Assisted Solution

by:Randy Downs
Randy Downs earned 100 total points
ID: 38740784
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38740796
You can use this Regex and then remove the 2 slashes at the beginning:

//(?:.(?!//))+$

Open in new window

0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 38740815
Dim myString As String = "abc//def//hij//I am the owner"
Dim arr As String()
arr = myString.Split(CChar("//")
messagebox.show(arr(arr.Length).ToString

Open in new window

0
 
LVL 15

Assisted Solution

by:David L. Hansen
David L. Hansen earned 150 total points
ID: 38740832
In C#:
string myString = null;
myString = "abc//def//hij//I am the owner";
string[] arr = null;
arr = myString.Split(Convert.ToChar("//"));
messagebox.show(arr(arr.Length)).ToString();

Open in new window

0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 38740872
Sorry, this should work:
        Dim myString As String = "abc//def//hij//I am the owner"
        Dim arr As String()
        myString.Replace("//", "#")
        arr = myString.Split(CChar("#"))
        MessageBox.Show(arr(arr.Length)).ToString()

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 150 total points
ID: 38740889
Dim input As String = "abc//def//hij//I am the owner"
            Dim result As String

            result = input.Substring(input.LastIndexOf("//") + 2)
0
 

Accepted Solution

by:
Dovberman earned 0 total points
ID: 38741317
There were several answers, but this is what worked for me.

             intFileCount += 1;
                strFileName = FileName;  // Full path of filename

                // strFileName1 is the actual file name is after last occurance of '\\'

                intLastPos = strFileName.LastIndexOf("\\");
                intStartPos = intLastPos+2;
                intFileNameLength = strFileName.Length;

                strFileName1 = strFileName.Substring(intStartPos, (intFileNameLength - intStartPos));
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38742257
No close, @Number-1 posted link to the answer.

Accept this one.
http://www.experts-exchange.com/Programming/Languages/.NET/Q_27983783.html#a38740784
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38742258
See last comment
0
 

Author Closing Comment

by:Dovberman
ID: 38754098
Thanks for all the responses. These gave me hints for working out a solution.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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

21 Experts available now in Live!

Get 1:1 Help Now