Solved

Find last occurance of a substring within a string

Posted on 2013-01-03
10
429 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
[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
  • 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
Industry Leaders: 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!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
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…

740 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