Example usage for java.lang StringBuilder lastIndexOf

List of usage examples for java.lang StringBuilder lastIndexOf

Introduction

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

Prototype

@Override
    public int lastIndexOf(String str) 

Source Link

Usage

From source file:Main.java

public static void main(String[] arg) {
    StringBuilder phrase = new StringBuilder("java2s.com");
    int position = phrase.lastIndexOf(".com");

    System.out.println(position);
}

From source file:txyd.util.StringUtils.java

public static void main(String[] args) {
    System.out.println(5 % 5);//  ww  w.  j a v  a  2  s . c o m
    System.out.println(isLong("1111111111111111111111"));
    StringBuilder sb = new StringBuilder("1_2_3_4_5_");
    System.out.println(sb.substring(0, sb.lastIndexOf("_")));
}

From source file:com.admc.jcreole.JCreole.java

/**
 * Run this method with no parameters to see syntax requirements and the
 * available parameters.//w  w  w .j ava2s .  c om
 *
 * N.b. do not call this method from a persistent program, because it
 * may call System.exit!
 * <p>
 * Any long-running program should use the lower-level methods in this
 * class instead (or directly use CreoleParser and CreoleScanner
 * instances).
 * </p> <p>
 * This method executes with all JCreole privileges.
 * </p> <p>
 * This method sets up the following htmlExpander mappings (therefore you
 * can reference these in both Creole and boilerplate text).<p>
 * <ul>
 *   <li>sys|*: mappings for Java system properties
 *   <li>isoDateTime
 *   <li>isoDate
 *   <li>pageTitle: Value derived from the specified Creole file name.
 * </ul>
 *
 * @throws IOException for any I/O problem that makes it impossible to
 *         satisfy the request.
 * @throws CreoleParseException
 *         if can not generate output, or if the run generates 0 output.
 *         If the problem is due to input formatting, in most cases you
 *         will get a CreoleParseException, which is a RuntimException, and
 *         CreoleParseException has getters for locations in the source
 *         data (though they will be offset for \r's in the provided
 *         Creole source, if any).
 */
public static void main(String[] sa) throws IOException {
    String bpResPath = null;
    String bpFsPath = null;
    String outPath = null;
    String inPath = null;
    boolean debugs = false;
    boolean troubleshoot = false;
    boolean noBp = false;
    int param = -1;
    try {
        while (++param < sa.length) {
            if (sa[param].equals("-d")) {
                debugs = true;
                continue;
            }
            if (sa[param].equals("-t")) {
                troubleshoot = true;
                continue;
            }
            if (sa[param].equals("-r") && param + 1 < sa.length) {
                if (bpFsPath != null || bpResPath != null || noBp)
                    throw new IllegalArgumentException();
                bpResPath = sa[++param];
                continue;
            }
            if (sa[param].equals("-f") && param + 1 < sa.length) {
                if (bpResPath != null || bpFsPath != null || noBp)
                    throw new IllegalArgumentException();
                bpFsPath = sa[++param];
                continue;
            }
            if (sa[param].equals("-")) {
                if (noBp || bpFsPath != null || bpResPath != null)
                    throw new IllegalArgumentException();
                noBp = true;
                continue;
            }
            if (sa[param].equals("-o") && param + 1 < sa.length) {
                if (outPath != null)
                    throw new IllegalArgumentException();
                outPath = sa[++param];
                continue;
            }
            if (inPath != null)
                throw new IllegalArgumentException();
            inPath = sa[param];
        }
        if (inPath == null)
            throw new IllegalArgumentException();
    } catch (IllegalArgumentException iae) {
        System.err.println(SYNTAX_MSG);
        System.exit(1);
    }
    if (!noBp && bpResPath == null && bpFsPath == null)
        bpResPath = DEFAULT_BP_RES_PATH;
    String rawBoilerPlate = null;
    if (bpResPath != null) {
        if (bpResPath.length() > 0 && bpResPath.charAt(0) == '/')
            // Classloader lookups are ALWAYS relative to CLASSPATH roots,
            // and will abort if you specify a beginning "/".
            bpResPath = bpResPath.substring(1);
        InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(bpResPath);
        if (iStream == null)
            throw new IOException("Boilerplate inaccessible: " + bpResPath);
        rawBoilerPlate = IOUtil.toString(iStream);
    } else if (bpFsPath != null) {
        rawBoilerPlate = IOUtil.toString(new File(bpFsPath));
    }
    String creoleResPath = (inPath.length() > 0 && inPath.charAt(0) == '/') ? inPath.substring(1) : inPath;
    // Classloader lookups are ALWAYS relative to CLASSPATH roots,
    // and will abort if you specify a beginning "/".
    InputStream creoleStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(creoleResPath);
    File inFile = (creoleStream == null) ? new File(inPath) : null;
    JCreole jCreole = (rawBoilerPlate == null) ? (new JCreole()) : (new JCreole(rawBoilerPlate));
    if (debugs) {
        jCreole.setInterWikiMapper(new InterWikiMapper() {
            // This InterWikiMapper is just for prototyping.
            // Use wiki name of "nil" to force lookup failure for path.
            // Use wiki page of "nil" to force lookup failure for label.
            public String toPath(String wikiName, String wikiPage) {
                if (wikiName != null && wikiName.equals("nil"))
                    return null;
                return "{WIKI-LINK to: " + wikiName + '|' + wikiPage + '}';
            }

            public String toLabel(String wikiName, String wikiPage) {
                if (wikiPage == null)
                    throw new RuntimeException("Null page name sent to InterWikiMapper");
                if (wikiPage.equals("nil"))
                    return null;
                return "{LABEL for: " + wikiName + '|' + wikiPage + '}';
            }
        });
        Expander creoleExpander = new Expander(Expander.PairedDelims.RECTANGULAR);
        creoleExpander.put("testMacro",
                "\n\n<<prettyPrint>>\n{{{\n" + "!/bin/bash -p\n\ncp /etc/inittab /tmp\n}}}\n");
        jCreole.setCreoleExpander(creoleExpander);
    }
    jCreole.setPrivileges(EnumSet.allOf(JCreolePrivilege.class));
    Expander exp = jCreole.getHtmlExpander();
    Date now = new Date();
    exp.putAll("sys", System.getProperties(), false);
    exp.put("isoDateTime", isoDateTimeFormatter.format(now), false);
    exp.put("isoDate", isoDateFormatter.format(now), false);
    exp.put("pageTitle",
            (inFile == null) ? creoleResPath.replaceFirst("[.][^.]*$", "").replaceFirst(".*[/\\\\.]", "")
                    : inFile.getName().replaceFirst("[.][^.]*$", ""));
    if (troubleshoot) {
        // We don't write any HMTL output here.
        // Goal is just to output diagnostics.
        StringBuilder builder = (creoleStream == null) ? IOUtil.toStringBuilder(inFile)
                : IOUtil.toStringBuilder(creoleStream);
        int newlineCount = 0;
        int lastOffset = -1;
        int offset = builder.indexOf("\n");
        for (; offset >= 0; offset = builder.indexOf("\n", offset + 1)) {
            lastOffset = offset;
            newlineCount++;
        }
        // Accommodate input files with no terminating newline:
        if (builder.length() > lastOffset + 1)
            newlineCount++;
        System.out.println("Input lines:  " + newlineCount);
        Exception lastException = null;
        while (true) {
            try {
                jCreole.parseCreole(builder);
                break;
            } catch (Exception e) {
                lastException = e;
            }
            if (builder.charAt(builder.length() - 1) == '\n')
                builder.setLength(builder.length() - 1);
            offset = builder.lastIndexOf("\n");
            if (offset < 1)
                break;
            newlineCount--;
            builder.setLength(builder.lastIndexOf("\n"));
        }
        System.out.println((lastException == null) ? "Input validates"
                : String.format("Error around input line %d:  %s", newlineCount, lastException.getMessage()));
        return;
    }
    String generatedHtml = (creoleStream == null) ? jCreole.parseCreole(inFile)
            : jCreole.parseCreole(IOUtil.toStringBuilder(creoleStream));
    String html = jCreole.postProcess(generatedHtml, SystemUtils.LINE_SEPARATOR);
    if (outPath == null) {
        System.out.print(html);
    } else {
        FileUtils.writeStringToFile(new File(outPath), html, "UTF-8");
    }
}

From source file:Main.java

public static File convertFormat(File file, String newFormat) {
    StringBuilder tmpValue = new StringBuilder(file.getAbsolutePath());
    int index = tmpValue.lastIndexOf(".") + 1;
    tmpValue.delete(index, tmpValue.length());
    tmpValue.append(newFormat);//from   www. java  2 s  .  c o  m
    File result = new File(tmpValue.toString());

    return result;
}

From source file:Main.java

public static <T> String parseArrayToString(T[] imgsIds) {
    StringBuilder stringBuilder = new StringBuilder();
    for (T t : imgsIds) {
        stringBuilder.append(t).append(",");
    }//from w w w  .  jav a  2s.c o m
    int index = stringBuilder.lastIndexOf(",");
    if (index == (stringBuilder.toString().length() - 1)) {
        stringBuilder.deleteCharAt(index);
    }
    return stringBuilder.toString();
}

From source file:hr.fer.spocc.export.DotExporter.java

private static void reduceTreePrefix(StringBuilder prefix) {
    int index = prefix.lastIndexOf(".");
    if (index != -1)
        prefix.delete(index, prefix.length());
}

From source file:Main.java

public static String getTags(List<String> tags, String block) {
    int tagSize = tags.size();
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < tagSize; i++) {
        result.append(tags.get(i) + block);
    }/*from   w w  w. ja va2 s .co m*/
    result.deleteCharAt(result.lastIndexOf(block));
    return result.toString();
}

From source file:Main.java

public static <T> String listToString(List<T> list) {
    if (null == list || list.isEmpty()) {
        return "";
    }/* w  ww . j  a  v a 2  s  .co  m*/
    StringBuilder sb = new StringBuilder();
    for (T i : list) {
        sb.append(i.toString());
        sb.append(",");
    }
    int index = sb.lastIndexOf(",");
    if (-1 != index) {
        sb.deleteCharAt(index);
    }
    return sb.toString();
}

From source file:com.jk.util.JKStringUtil.java

/**
 * Removes the last.//from  w w  w . j  a  va  2  s .c o  m
 *
 * @param builder
 *            the builder
 * @param string
 *            the string
 * @return the string builder
 */
////////////////////////////////////////////////////////////////////////////////////////////
public static StringBuilder removeLast(final StringBuilder builder, final String string) {
    final int lastIndexOf = builder.lastIndexOf(string);
    if (lastIndexOf == -1) {
        return builder;
    }
    return new StringBuilder(builder.substring(0, lastIndexOf));
}

From source file:net.duckling.common.util.CommonUtils.java

/**??"," 
 * @param str ??//from w  ww.  j a  va2  s .c om
 * @param flag ","","
 * @return ??
 * */
public static String format(String str, String flag) {
    if (CommonUtils.isNull(str)) {
        return "";
    }
    StringBuilder sb = new StringBuilder(str);
    if (sb.indexOf(flag) > 0) {
        return sb.substring(0, sb.lastIndexOf(flag));
    }
    return sb.toString();
}