Example usage for java.lang String replace

List of usage examples for java.lang String replace

Introduction

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

Prototype

public String replace(CharSequence target, CharSequence replacement) 

Source Link

Document

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

Usage

From source file:jp.co.opentone.bsol.framework.test.util.ExpectedMessageStringGenerator.java

public static String generate(String msg, String actionName, Object... vars) {

    if (StringUtils.isNotEmpty(actionName)) {
        msg = msg.replace("$action$", "?[" + actionName + "]");
    }//from w ww. ja v  a2 s  . com

    assertTrue(!msg.matches("\\$action\\$"));

    String result = MessageFormat.format(msg, vars);
    assertTrue(!result.matches("\\{[0-9]+\\}"));

    return result;
}

From source file:Main.java

/**
 * Creates a {@link StreamResult} by wrapping the given outputStream in an
 * {@link OutputStreamWriter} that transforms Windows line endings (\r\n) 
 * into Unix line endings (\n) on Windows for consistency with Roo's templates.  
 * /*from w w w.  j  a  v a  2s . c o m*/
 * @param outputStream
 * @return StreamResult 
 * @throws UnsupportedEncodingException 
 */
private static StreamResult createUnixStreamResultForEntry(OutputStream outputStream)
        throws UnsupportedEncodingException {
    final Writer writer;
    if (System.getProperty("line.separator").equals("\r\n")) {
        writer = new OutputStreamWriter(outputStream, "ISO-8859-1") {
            public void write(char[] cbuf, int off, int len) throws IOException {
                for (int i = off; i < off + len; i++) {
                    if (cbuf[i] != '\r' || (i < cbuf.length - 1 && cbuf[i + 1] != '\n')) {
                        super.write(cbuf[i]);
                    }
                }
            }

            public void write(int c) throws IOException {
                if (c != '\r')
                    super.write(c);
            }

            public void write(String str, int off, int len) throws IOException {
                String orig = str.substring(off, off + len);
                String filtered = orig.replace("\r\n", "\n");
                int lengthDiff = orig.length() - filtered.length();
                if (filtered.endsWith("\r")) {
                    super.write(filtered.substring(0, filtered.length() - 1), 0, len - lengthDiff - 1);
                } else {
                    super.write(filtered, 0, len - lengthDiff);
                }
            }
        };
    } else {
        writer = new OutputStreamWriter(outputStream, "ISO-8859-1");
    }
    return new StreamResult(writer);
}

From source file:Main.java

/**
 * Escapes the given text such that it can be safely embedded in a string literal
 * in Java source code./*w w  w  .j  a v a  2s  . c  o  m*/
 * 
 * @param text the text to escape
 * 
 * @return the escaped text
 */
public static String escapeToJavaString(String text) {
    if (text == null) {
        return null;
    }
    String result = text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replace("\b", "\\b")
            .replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t");
    StringBuilder complete = new StringBuilder();
    for (int i = 0; i < result.length(); i++) {
        int codePointI = result.codePointAt(i);
        if (codePointI >= 32 && codePointI <= 127) {
            complete.append(Character.toChars(codePointI));
        } else {
            // use Unicode representation
            complete.append("\\u");
            String hex = Integer.toHexString(codePointI);
            complete.append(getRepeatingString(4 - hex.length(), '0'));
            complete.append(hex);
        }
    }
    return complete.toString();
}

From source file:Main.java

public static String encodeSpecialCharsForFileSystem(String projectName) {
    if (projectName.equals(".") || projectName.equals("..")) {
        projectName = projectName.replace(".", "%2E");
    } else {/* ww  w. j a  v  a  2s  . c om*/
        projectName = projectName.replace("%", "%25");
        projectName = projectName.replace("\"", "%22");
        projectName = projectName.replace("/", "%2F");
        projectName = projectName.replace(":", "%3A");
        projectName = projectName.replace("<", "%3C");
        projectName = projectName.replace(">", "%3E");
        projectName = projectName.replace("?", "%3F");
        projectName = projectName.replace("\\", "%5C");
        projectName = projectName.replace("|", "%7C");
        projectName = projectName.replace("*", "%2A");
    }
    return projectName;
}

From source file:com.autentia.common.util.ejb.JBossUtils.java

/**
 * Devuelve el nombre del ear en el que se encuentra la clase <code>clazz</code>. "" si no est en un ear.
 * <p>/*from   w ww. j a v a  2 s.co  m*/
 * Este mtodo esta probado para JBoss 4.2.2GA.
 * 
 * @param clazz clase que se est buscando.
 * @return el nombre del ear en el que se encuentra la clase <code>clazz</code>. "" si no est en un ear.
 */
public static String getEarName(Class<?> clazz) {
    String cn = File.separator + clazz.getCanonicalName();
    cn = cn.replace('.', File.separatorChar);
    cn += ".class";
    final URL url = Thread.currentThread().getContextClassLoader().getResource(cn);
    final String path = url.getPath();
    if (log.isDebugEnabled()) {
        log.debug(clazz.getCanonicalName() + " is in path: " + path);
    }
    final int indexOfEar = path.indexOf(".ear");
    final int indexOfExclamationChar = path.indexOf("!");
    if (indexOfEar > -1 && indexOfExclamationChar > -1 && indexOfEar < indexOfExclamationChar) {
        // JBoss despliega los ear en un directorio temporal del estilo: .../tmp34545nombreDelEar.ear-contents/...
        int beginTempDir = path.lastIndexOf(File.separatorChar, indexOfEar);
        int i = beginTempDir;
        boolean reachedDigit = false;
        while (i < indexOfEar) {
            if (Character.isDigit(path.charAt(i))) {
                reachedDigit = true;
            } else if (reachedDigit) {
                break;
            }
            i++;
        }
        final String prefix = path.substring(i, indexOfEar) + File.separator;
        log.debug(clazz.getCanonicalName() + " is inside ear: " + prefix);
        return prefix;
    }

    log.debug(clazz.getCanonicalName() + " is not inside an ear.");
    return "";
}

From source file:Main.java

public static String escapeSingleQuotes(final String inputString) {
    if (inputString == null) {
        throw new IllegalArgumentException("null inputString");
    }//from  ww w .  j a  va2s. c o m

    return inputString.replace("'", "\\'");

}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static byte[] getBytesFromHexString(String hexstring) {
    if (hexstring == null || hexstring.equals("")) {
        return null;
    }/*from   w w w  .j a v a  2  s  .  c o  m*/
    hexstring = hexstring.replace(" ", "");
    hexstring = hexstring.toUpperCase();
    int size = hexstring.length() / 2;
    char[] hexarray = hexstring.toCharArray();
    byte[] rv = new byte[size];
    for (int i = 0; i < size; i++) {
        int pos = i * 2;
        rv[i] = (byte) (charToByte(hexarray[pos]) << 4 | charToByte(hexarray[pos + 1]));
    }
    return rv;
}

From source file:Main.java

/**
 *  Get path to with out root directory//from   w ww. ja v  a  2 s  .  c o  m
 *  @param rootDir - root directory
 *  @param path - full path
 *  @return string to show current path
 */
public static final String getLabelPath(String rootDir, String path) {
    String result = "";
    if (path == null) {
        result = File.separator;
    } else {
        result = path.replace(rootDir, "");
    }
    return result;
}

From source file:com.hp.autonomy.hod.client.api.resource.ResourceName.java

private static String unescapeComponent(final String input) {
    return input.replace("\\:", ":").replace("\\\\", "\\");
}

From source file:com.esri.geoevent.test.performance.utils.MessageUtils.java

public static String escapeNewLineCharacters(String data) {
    if (StringUtils.isEmpty(data))
        return null;

    String replacedData = data.replace(DEFAULT_CRNL_SEPERATOR, CRNL_SEPERATOR);
    replacedData = replacedData.replace(DEFAULT_CR_SEPERATOR, CR_SEPERATOR);
    replacedData = replacedData.replace(DEFAULT_NL_SEPERATOR, NL_SEPERATOR);

    return replacedData;
}