Solved

Find last occurance of a substring within a string

Posted on 2013-01-03
10
431 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 30

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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