getting the last part of a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raids51
    New Member
    • Nov 2007
    • 59

    getting the last part of a string

    i need help. i have vb 2005 and i want to take a string and only use every thing past the last "/" ex. "C:\example\exa mple\THIS-IS-THE-PART-I-WANT.txt"
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    there is a method called LastIndexOf for a string dataType.
    use that to get the index of the required charachter.

    Alternatively you may also use the Split method.

    Comment

    • vishalgupta
      New Member
      • Jun 2007
      • 47

      #3
      you can call the Split() as / as the delimiter
      using UBound you can access the last element of the array formed by split() which is what you want to extract....

      Comment

      • raids51
        New Member
        • Nov 2007
        • 59

        #4
        first i just dont understand what ur talkin about with the lastindexof part and second i could use split but there is more then one "/" in the string any more ideas/details

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #5
          Split returns an array of strings seperated by the charachters.

          get the last array index and that value....which will be what you are looking for

          [CODE=cpp]string str = "hello\im\here" ;
          int i = str.LastIndexOf ('\');[/CODE]

          i will be 9 in this case

          cheers
          use intellisense and msdn before you start throwing comments

          Comment

          • kunal pawar
            Contributor
            • Oct 2007
            • 297

            #6
            Shashi is right
            u can use

            string str = "C:\example\exa mple\THIS-IS-THE-PART-I-WANT.txt";
            int i = str.LastIndexOf ('\');
            string s ="";
            s= str.substr(i+1, str.length) ;

            OUTPUT will be
            THIS-IS-THE-PART-I-WANT.txt

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Another option is, if you are using this to only find filenames from a full file path, is to use the FileInfo class and use the .Name property to get the filename.

              Comment

              Working...