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:MainClass.java

public static void saveBinaryFile(URL u) {
    int bufferLength = 128;
    try {/*from   w  ww .ja  va 2  s  . c om*/
        URLConnection uc = u.openConnection();
        String ct = uc.getContentType();
        int contentLength = uc.getContentLength();
        if (ct.startsWith("text/") || contentLength == -1) {
            System.err.println("This is not a binary file.");
            return;
        }

        InputStream stream = uc.getInputStream();
        byte[] buffer = new byte[contentLength];
        int bytesread = 0;
        int offset = 0;
        while (bytesread >= 0) {
            bytesread = stream.read(buffer, offset, bufferLength);
            if (bytesread == -1)
                break;
            offset += bytesread;
        }
        if (offset != contentLength) {
            System.err.println("Error: Only read " + offset + " bytes");
            System.err.println("Expected " + contentLength + " bytes");
        }

        String theFile = u.getFile();
        theFile = theFile.substring(theFile.lastIndexOf('/') + 1);
        FileOutputStream fout = new FileOutputStream(theFile);
        fout.write(buffer);
    } catch (Exception e) {
        System.err.println(e);
    }
    return;
}

From source file:Main.java

public static String getFileNameFromUrl(String url) {
    if (!TextUtils.isEmpty(url)) {
        return url.substring(url.lastIndexOf("/") + 1);
    }//from   w w  w .  ja  v a 2  s .  com

    return System.currentTimeMillis() + "";
}

From source file:Main.java

private static String removeLast(String s, String g) {
    if (s.contains(g)) {
        int index = s.lastIndexOf(g);
        int indexEnd = index + g.length();
        if (index == 0) {
            return s.substring(1);
        } else if (index == (s.length() - 1)) {
            return s.substring(0, index);
        } else {//from   w  ww . j  a  va  2s  .c  o m
            return s.substring(0, index) + s.substring(indexEnd);
        }
    }
    return s;
}

From source file:Main.java

/**
 * from "myFunction(arg1,arg2)", return ["arg1","arg2"]
 *//*ww w.  j a  v  a2  s . com*/
public static String[] getAttributes(String tag) {
    String attributes = tag.substring(tag.indexOf('(') + 1, tag.lastIndexOf(')'));
    if (attributes.isEmpty())
        return new String[0];
    return trim(attributes.split(","));
}

From source file:Main.java

public static String getFileNameWithNoSuffix(String path) {
    String name = null;/*from  w w  w. j a  v a2  s .com*/
    int index = path.lastIndexOf('/');
    if (index > 0) {
        name = path.substring(index + 1, path.length());
    }
    int index1 = name.lastIndexOf('.');
    if (index1 > 0) {
        name = name.substring(0, index1);
    }
    return name;
}

From source file:Main.java

static public String stripTopLevelTag(String xmlString) {
    return xmlString.substring(xmlString.indexOf('>') + 1, xmlString.lastIndexOf('<'));
}

From source file:Main.java

public static String removeGeneric(String s) {
    if (s.contains("<"))
        return s.substring(0, s.lastIndexOf("<"));
    else/* w  w  w. j a  va 2s .  co m*/
        return s;
}

From source file:Main.java

static String getFilePathFromUrl(final Context c, final String url) {
    return getDir(c).getAbsoluteFile() + "/" + url.substring(url.lastIndexOf("/") + 1);
}

From source file:Main.java

public static String clazzName(final File base, final File file) {
    final int rootLength = base.getAbsolutePath().length();
    final String absFileName = file.getAbsolutePath();
    final int p = absFileName.lastIndexOf('.');
    final String relFileName = absFileName.substring(rootLength + 1, p);
    return relFileName.replace(File.separatorChar, '.');
}

From source file:Main.java

public static String removeLastIdentifier(String s) {
    if (s.contains("."))
        return s.substring(0, s.lastIndexOf("."));
    else/*from w  w  w . j  av a2s  . c  om*/
        return new String();
}