Example usage for java.lang String lastIndexOf

List of usage examples for java.lang String lastIndexOf

Introduction

In this page you can find the example usage for java.lang String lastIndexOf.

Prototype

public int lastIndexOf(String str) 

Source Link

Document

Returns the index within this string of the last occurrence of the specified substring.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.util.Map.Entry.class;
    String name = cls.getName();
    if (name.lastIndexOf('.') > 0) {
        name = name.substring(name.lastIndexOf('.') + 1); // Map$Entry
        name = name.replace('$', '.'); // Map.Entry
    }//w  w w  .j a va  2 s .c o  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String string = "I am Java";

    int index = string.lastIndexOf("am");

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String string = "I am Java";

    int index = string.lastIndexOf("z"); // -1

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String string = "this is another test.";
    int index = string.lastIndexOf('z');
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String string = "this is another test.";
    int index = string.lastIndexOf('z'); // -1
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String string = "this is another test. a";
    int index = string.lastIndexOf('a');
}

From source file:Main.java

public static void main(String[] args) {
    File f = new File("c:/test");
    FilenameFilter fileNameFilter = new FilenameFilter() {
        @Override/* w w w . ja  v a2 s  . c o m*/
        public boolean accept(File dir, String name) {
            if (name.lastIndexOf('.') > 0) {
                int lastIndex = name.lastIndexOf('.');
                String str = name.substring(lastIndex);
                if (str.equals(".txt")) {
                    return true;
                }
            }
            return false;
        }
    };
    File[] paths = f.listFiles(fileNameFilter);
    for (File path : paths) {
        System.out.println(path);
    }
}

From source file:MainClass.java

public static void main(String[] arg) {
    String str = "abcdea";

    int index = 0;
    index = str.lastIndexOf('a');

    System.out.println(index);/*from  w  w  w.  jav a  2s. c  o m*/
}

From source file:MainClass.java

public static void main(String[] arg) {
    String str = "abcdeabcdef";

    int index = 0;
    index = str.lastIndexOf("ab");

    System.out.println(index);//from   w ww.j a  v  a  2 s. c  o  m
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com/binary.dat");
    URLConnection uc = u.openConnection();
    String contentType = uc.getContentType();
    int contentLength = uc.getContentLength();
    if (contentType.startsWith("text/") || contentLength == -1) {
        throw new IOException("This is not a binary file.");
    }/*from   ww w . j a  v  a 2s  . c o  m*/
    InputStream raw = uc.getInputStream();
    InputStream in = new BufferedInputStream(raw);
    byte[] data = new byte[contentLength];
    int bytesRead = 0;
    int offset = 0;
    while (offset < contentLength) {
        bytesRead = in.read(data, offset, data.length - offset);
        if (bytesRead == -1)
            break;
        offset += bytesRead;
    }
    in.close();

    if (offset != contentLength) {
        throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
    }

    String filename = u.getFile().substring(filename.lastIndexOf('/') + 1);
    FileOutputStream out = new FileOutputStream(filename);
    out.write(data);
    out.flush();
    out.close();
}