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
Solved

Find last occurance of a substring within a string

Posted on 2013-01-03
10
428 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# LINQ ForEach() question 6 54
Infrastructure development vs software development 3 60
Add or delete table rows 10 53
how to just get time from a date 6 32
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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

840 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