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

/**
 * get XML text for name and value of attribute. The value's < and > gets escaped (ampersands do not, for compatibility reasons).
 *
 * @param name/*www . j  a v a 2s  . com*/
 * @param value
 * @deprecated Thou shall not produce XML by hand.
 */
public static String wrapatt(final String name, final String value) {
    final String escapedValue = value.replace(">", "&gt;").replace("<", "&lt;");
    return " " + name + "=" + "\"" + escapedValue + "\"";
}

From source file:Main.java

public static Document getdoc(String uri) throws Exception {
    uri = uri.replace(" ", "%20");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;/* w w  w .  j  a v  a 2 s.c  o m*/
    db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    doc = db.parse(new URL(uri).openStream());
    if (doc == null || doc.getTextContent() == "") {
        throw new Exception();
    } else
        return doc;
}

From source file:Main.java

/**
 * Encode a string to be able to use it in an IMAP command.
 *
 * "A quoted string is a sequence of zero or more 7-bit characters,
 *  excluding CR and LF, with double quote (<">) characters at each
 *  end." - Section 4.3, RFC 3501/* ww  w  .  j  av a 2s  . c  o m*/
 *
 * Double quotes and backslash are escaped by prepending a backslash.
 *
 * @param str
 *     The input string (only 7-bit characters allowed).
 * @return
 *     The string encoded as quoted (IMAP) string.
 */
public static String encodeString(String str) {
    return "\"" + str.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}

From source file:Main.java

public static String replaceWords(String phoneNumber) {

    String added_phoneNo = phoneNumber.replace(" ", "").replace("+", "").replace("-", "").replace("(", "")
            .replace(")", "");
    //      if(added_phoneNo.length() > 10) {
    //         added_phoneNo = added_phoneNo.substring(added_phoneNo.length() - 10);
    //         //from  w ww  .  j av  a 2  s.c  om
    //      }
    return added_phoneNo;

}

From source file:Main.java

public static String convertHTMLTagsForDisplay(String source) {

    // bold//from  w  ww.  j  a va  2 s  .c om
    source = source.replace("<strong>", "<b>");
    source = source.replace("</strong>", "</b>");

    // italics
    source = source.replace("<em>", "<i>");
    source = source.replace("</em>", "</i>");

    return source;

}

From source file:Main.java

public static boolean validateCardNumber(String n) {
    n = n.replace(" ", "");
    int j = n.length();

    String[] s1 = new String[j];
    for (int i = 0; i < n.length(); i++)
        s1[i] = "" + n.charAt(i);

    int checksum = 0;

    for (int i = s1.length - 1; i >= 0; i -= 2) {
        int k = 0;

        if (i > 0) {
            k = Integer.valueOf(s1[i - 1]) * 2;
            if (k > 9) {
                String s = "" + k;
                k = Integer.valueOf(s.substring(0, 1)) + Integer.valueOf(s.substring(1));
            }/*from  www .j  a v a2s .co  m*/
            checksum += Integer.valueOf(s1[i]) + k;
        } else
            checksum += Integer.valueOf(s1[0]);
    }

    return ((checksum % 10) == 0);
}

From source file:Main.java

public static String convertHTMLTagsForUpload(String source) {

    // bold/*from  w  w w. j av a 2 s . c o m*/
    source = source.replace("<b>", "<strong>");
    source = source.replace("</b>", "</strong>");

    // italics
    source = source.replace("<i>", "<em>");
    source = source.replace("</i>", "</em>");

    return source;

}

From source file:Main.java

public static String[] splitToPairs(String Line) {
    Line = Line.replace(" + ", ", ");
    ArrayList<String> result = new ArrayList<>();
    boolean foundQwote = false;
    String pair = "";
    for (int idx = 0; idx < Line.length(); idx++) {
        if (Line.charAt(idx) == '"') {
            foundQwote = !foundQwote;/*w w w.  j  a v  a  2s  . co m*/
        } else if (Line.charAt(idx) == ',') {
            if (!foundQwote) {
                result.add(pair.trim());
                pair = "";
            } else {
                pair = pair + Line.charAt(idx);
            }
        } else {
            pair = pair + Line.charAt(idx);
        }
    }
    result.add(pair.trim());
    return result.toArray(new String[result.size()]);
}

From source file:Main.java

@Nonnull
public static String normalizeNewlines(@Nonnull String source, String newlineValue) {
    return source.replace("\r", "").replace("\n", newlineValue);
}

From source file:Main.java

public static String getUsernameFromAddress(String str) {
    if (str.contains("sip:")) {
        str = str.replace("sip:", "");
    }/* w  ww. ja  va 2 s  . co m*/
    return str.contains("@") ? str.split("@")[0] : str;
}