Example usage for java.lang StringBuilder replace

List of usage examples for java.lang StringBuilder replace

Introduction

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

Prototype

@Override
public StringBuilder replace(int start, int end, String str) 

Source Link

Usage

From source file:Main.java

public static String replace(String str, String target, String replacement) {
    StringBuilder sb = new StringBuilder(str);

    int index = sb.length();
    int lenTarget = target.length();
    while ((index = sb.lastIndexOf(target, index)) != -1) {
        sb.replace(index, index + lenTarget, replacement);
        index -= lenTarget;//  www. ja  va 2 s .co m
    }

    return sb.toString();
}

From source file:Main.java

public static Spannable replaceTags(String str, Context context) {
    try {//ww w. ja  v  a  2 s. c  o m
        int start = -1;
        int startColor = -1;
        int end = -1;
        StringBuilder stringBuilder = new StringBuilder(str);
        while ((start = stringBuilder.indexOf("<br>")) != -1) {
            stringBuilder.replace(start, start + 4, "\n");
        }
        while ((start = stringBuilder.indexOf("<br/>")) != -1) {
            stringBuilder.replace(start, start + 5, "\n");
        }
        ArrayList<Integer> bolds = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        while ((start = stringBuilder.indexOf("<b>")) != -1
                || (startColor = stringBuilder.indexOf("<c")) != -1) {
            if (start != -1) {
                stringBuilder.replace(start, start + 3, "");
                end = stringBuilder.indexOf("</b>");
                stringBuilder.replace(end, end + 4, "");
                bolds.add(start);
                bolds.add(end);
            } else if (startColor != -1) {
                stringBuilder.replace(startColor, startColor + 2, "");
                end = stringBuilder.indexOf(">", startColor);
                int color = Color.parseColor(stringBuilder.substring(startColor, end));
                stringBuilder.replace(startColor, end + 1, "");
                end = stringBuilder.indexOf("</c>");
                stringBuilder.replace(end, end + 4, "");
                colors.add(startColor);
                colors.add(end);
                colors.add(color);
            }
        }
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(stringBuilder);
        for (int a = 0; a < colors.size() / 3; a++) {
            spannableStringBuilder.setSpan(new ForegroundColorSpan(colors.get(a * 3 + 2)), colors.get(a * 3),
                    colors.get(a * 3 + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } catch (Exception e) {

    }
    return new SpannableStringBuilder(str);
}

From source file:com.kotcrab.vis.editor.util.FileUtils.java

/** Replace path extension eg. replaceExtension("/gfx/game.atlas", "png") would return "/gfx/game.png" */
public static String replaceExtension(String path, String newExtension) {
    StringBuilder builder = new StringBuilder(path);
    builder.replace(path.lastIndexOf('.'), path.length(), '.' + newExtension);
    return builder.toString();
}

From source file:Main.java

public static String convertStreamToString(InputStream is) throws Exception {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;//from  ww w .  j av  a  2s .c  o m
    String NL = System.getProperty("line.separator");
    while ((line = reader.readLine()) != null) {
        sb.append(line + NL);
    }
    sb.replace(sb.length() - 1, sb.length(), "");
    is.close();
    return sb.toString();
}

From source file:cn.loveapple.service.util.service.ServiceUtil.java

/**
 * <code>cn.loveapple.service.cool.model</code>??????
 * <code>cn.loveapple.service.cool.meta</code>????
 * // w  w w.  jav  a  2  s . co m
 * 
 * @param modelName ??????
 * @return ???
 */
public static ModelMeta getModelMeta(String modelName) {
    if (StringUtils.isEmpty(modelName)) {
        throw new IllegalArgumentException("ModelMeta key is empty.");
    }
    if (metaStorage.get(modelName) == null) {
        StringBuilder metaClass = new StringBuilder(modelName.length());
        metaClass.append(modelName);
        metaClass.append("Meta");
        metaClass.replace(26, 31, "meta");

        Method getMethod = null;
        try {
            getMethod = Class.forName(metaClass.toString()).getMethod("get");
        } catch (Exception e) {
            throw new RuntimeException("create meta method class error.", e);
        }

        try {
            metaStorage.put(modelName, (ModelMeta) getMethod.invoke(null));
        } catch (Exception e) {
            throw new RuntimeException("invoke meta method class error.", e);
        }
    }

    return metaStorage.get(modelName);
}

From source file:Main.java

public static void replaceAll(StringBuilder sb, String source, String target) {
    int index = 0;
    int endIndex = 0;
    int length = source.length();
    while (-1 != (index = sb.indexOf(source, index))) {
        endIndex = index + length;// w  w w . j av a 2 s .c  om
        sb.replace(index, endIndex, target);
        index += target.length() + 5;
    }
}

From source file:com.freiheit.fuava.sftp.util.FilenameUtil.java

/**
 * Returns the filename of the data file for given ok file.
 *///w w  w.  ja v  a 2s.  c  o  m
@CheckForNull
public static String getDataFileOfOkFile(@Nonnull final FileType type, @Nonnull final String filename) {
    if (filename.endsWith(type.getOkFileExtention())) {
        final StringBuilder b = new StringBuilder(filename);
        b.replace(filename.lastIndexOf(type.getOkFileExtention()),
                filename.lastIndexOf(type.getOkFileExtention()) + type.getOkFileExtention().length(),
                type.getExtention());
        return b.toString();
    }

    return null;
}

From source file:com.freiheit.fuava.sftp.util.FilenameUtil.java

/**
 * Returns the filename of the data file for given ok file.
 *//*from   w w w .  ja  v  a 2s  .c om*/
@CheckForNull
public static String getOkFileForDataFile(@Nonnull final FileType type, @Nonnull final String filename) {
    if (filename.endsWith(type.getExtention())) {
        final StringBuilder b = new StringBuilder(filename);
        b.replace(filename.lastIndexOf(type.getExtention()),
                filename.lastIndexOf(type.getExtention()) + type.getExtention().length(),
                type.getOkFileExtention());
        return b.toString();
    }

    return null;
}

From source file:com.aurel.track.exchange.docx.exporter.PreprocessImage.java

private static Integer parseNumber(String imgSrc, String tag) {
    int workItemIndex = imgSrc.indexOf(tag);
    if (workItemIndex != -1) {
        String workItemString = imgSrc.substring(workItemIndex);
        StringBuilder stringBuilder = new StringBuilder(workItemString);
        stringBuilder.replace(0, tag.length(), "");
        int i = 0;
        char charValue;
        StringBuilder numberString = new StringBuilder();
        do {/* w w w. j a  v a2  s .c o m*/
            charValue = stringBuilder.charAt(i++);
            if (Character.isDigit(charValue)) {
                numberString.append(charValue);
            }
        } while (stringBuilder.length() > i && Character.isDigit(charValue));
        return Integer.decode(numberString.toString());
    }
    return null;
}

From source file:Main.java

public static SpannableStringBuilder replaceTags(String str, int flag) {
    try {//from  w  w w. j a v a 2  s.  c o m
        int start;
        int end;
        StringBuilder stringBuilder = new StringBuilder(str);
        if ((flag & FLAG_TAG_BR) != 0) {
            while ((start = stringBuilder.indexOf("<br>")) != -1) {
                stringBuilder.replace(start, start + 4, "\n");
            }
            while ((start = stringBuilder.indexOf("<br/>")) != -1) {
                stringBuilder.replace(start, start + 5, "\n");
            }
        }
        ArrayList<Integer> bolds = new ArrayList<>();
        if ((flag & FLAG_TAG_BOLD) != 0) {
            while ((start = stringBuilder.indexOf("<b>")) != -1) {
                stringBuilder.replace(start, start + 3, "");
                end = stringBuilder.indexOf("</b>");
                if (end == -1) {
                    end = stringBuilder.indexOf("<b>");
                }
                stringBuilder.replace(end, end + 4, "");
                bolds.add(start);
                bolds.add(end);
            }
        }
        ArrayList<Integer> colors = new ArrayList<>();
        if ((flag & FLAG_TAG_COLOR) != 0) {
            while ((start = stringBuilder.indexOf("<c#")) != -1) {
                stringBuilder.replace(start, start + 2, "");
                end = stringBuilder.indexOf(">", start);
                int color = Color.parseColor(stringBuilder.substring(start, end));
                stringBuilder.replace(start, end + 1, "");
                end = stringBuilder.indexOf("</c>");
                stringBuilder.replace(end, end + 4, "");
                colors.add(start);
                colors.add(end);
                colors.add(color);
            }
        }
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(stringBuilder);
        // for (int a = 0; a < bolds.size() / 2; a++) {
        // spannableStringBuilder.setSpan(
        // new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")),
        // bolds.get(a * 2), bolds.get(a * 2 + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // }
        for (int a = 0; a < colors.size() / 3; a++) {
            spannableStringBuilder.setSpan(new ForegroundColorSpan(colors.get(a * 3 + 2)), colors.get(a * 3),
                    colors.get(a * 3 + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new SpannableStringBuilder(str);
}