The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

RE: Substring

Posted by Kishori Sharan on August 20, 2000 at 1:07 AM

Hi Kurram
First get the index of the last slash. Whenever you want to know the index of the first occurrance a character in a string from the end of the string you have to use lastIndexOf () method of String class. Then to get the string after last slash you have to use substring ( ) method. I have writtem a small program below. Hope it will help you. Instead of first storing the string in an object of string class you can also use a code like this
filename = "C:/kurram/file1.tif".substring ( "c:/kurram/file1.tif".lastIndexOf ( "/" ) + 1 ) ;

I mean you can also treat string literal ( an string withing quotes e.g. "kurram" ) as an object of String class and you can call a method of String class on them as shown above.
Thanx
Kishori
//////////FileName.java//////
class FileName {
public static void main ( String[] args ) {
String fpath = "C:/kishori/file1.gif" ;

// Get the index of last slash
int lastSlashPos = fpath.lastIndexOf ( "/" ) ;

// Here you should check for lastSlashPos value > 0
String fname = fpath.substring ( lastSlashPos +1 ) ;
System.out.println ( fname ) ;
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us