Java String Replace replaceOnce(String template, String placeholder, String replacement)

Here you can find the source of replaceOnce(String template, String placeholder, String replacement)

Description

replace Once

License

Apache License

Declaration

public static String replaceOnce(String template, String placeholder, String replacement) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;

public class Main {
    public static String replaceOnce(String template, String placeholder, String replacement) {
        if (template == null) {
            return template; // returning null!
        }//from   w  w  w. j  a  v a  2 s  .  c o  m
        int loc = template.indexOf(placeholder);
        if (loc < 0) {
            return template;
        } else {
            return new StringBuffer(template.substring(0, loc)).append(replacement)
                    .append(template.substring(loc + placeholder.length())).toString();
        }
    }

    public static String toString(Object[] objects) {
        return join(", ", objects);
    }

    public static String join(String seperator, Iterator objects) {
        StringBuffer sb = new StringBuffer();
        if (objects.hasNext())
            sb.append(objects.next());
        while (objects.hasNext()) {
            sb.append(seperator).append(objects.next());
        }
        return sb.toString();
    }

    public static String join(String seperator, Object[] objects) {
        int len = objects.length;
        StringBuffer sb = new StringBuffer(len * 2);
        if (len > 0) {
            sb.append(objects[0]);
            for (int i = 1; i < len; i++) {
                sb.append(seperator).append(objects[i]);
            }
        }
        return sb.toString();
    }
}

Related

  1. replaceEmitKeys(String map, Collection newKeys)
  2. replaceFirst(String input, String search, String replacement)
  3. replaceIgnoreCase(String line, String oldString, String newString)
  4. replaceIgnoreCase(String source, String strBeReplace, String strReplaced)
  5. replaceNonPrintableAsciiCharacters(String str)
  6. replaceSign(String sourceStr, char startc, char endc, String replaceStr)
  7. replaceSpace(String str, int actualLength)
  8. replaceUnsafeNamespaceDelimiters(String input)
  9. replaceWildcards(String wildcardPattern)