Example usage for java.lang String trim

List of usage examples for java.lang String trim

Introduction

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

Prototype

public String trim() 

Source Link

Document

Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).

Usage

From source file:Main.java

public static String getHost(String url) {
    if (url == null || "".equals(url.trim()))
        return null;

    Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
    Matcher matcher = p.matcher(url);
    if (matcher.find())
        return matcher.group();
    else//  www  . ja v a  2 s  .  c o m
        return null;
}

From source file:Main.java

public static String getFIO(String f, String i, String o) {

    String res = "";

    for (String t : new String[] { f, i, o }) {
        if (t != null && !(t = t.trim()).isEmpty()) {
            res += (res.isEmpty() ? "" : " ") + t;
        }/*w w w . j av  a 2 s  . co  m*/
    }

    return res;
}

From source file:Main.java

private static String removeEmptyLines(String text) {
    StringBuffer buffer = new StringBuffer();
    String[] lines = text.split("\n");
    for (String string : lines) {
        if (string.trim().isEmpty()) {
            continue;
        }//from w w  w. j  a v a  2s  .  c o  m
        buffer.append(string + "\n");
    }
    return buffer.toString();
}

From source file:Main.java

public static XPathExpression getXPathExpression(String path) throws RuntimeException {
    path = path.trim();
    try {/*w w  w  .  j a  va2 s. c  o m*/
        XPath xp = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xp.compile(path);
        return expr;

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

}

From source file:Main.java

/**
 * Makes a given filename safe by replacing special characters like slashes ("/" and "\")
 * with dashes ("-").//from w  w w .j  a v  a2s.co  m
 *
 * @param filename The filename in question.
 * @return The filename with special characters replaced by hyphens.
 */
private static String fileSystemSafe(String filename) {
    if (filename == null || filename.trim().length() == 0) {
        return "unnamed";
    }

    for (String s : FILE_SYSTEM_UNSAFE) {
        filename = filename.replace(s, "-");
    }
    return filename;
}

From source file:Main.java

public static String getModel() {
    String model = Build.MODEL;
    if (model != null) {
        model = model.trim().replaceAll("\\s*", "");
    } else {// w  w w .  ja v  a  2 s .c  o m
        model = "";
    }
    return model;
}

From source file:Main.java

public static boolean isBlank(String src) {
    if (src == null)
        return true;
    src = src.trim();
    if (src == null)
        return true;
    if (src.equals(""))
        return true;
    return false;
}

From source file:Main.java

public static String getBehaviour(final String rawString) {
    return (isStringNonEmpty(rawString)) ? rawString.trim() : "Unavailable";
}

From source file:Main.java

/**
 *
 * @param jsonStr string param .expect a json string
 * @return formatted json string if param is a json string,otherwise return the param
 *//*from   w  w w.ja va 2s  .c o  m*/
public static String convert(String jsonStr) {
    try {
        jsonStr = jsonStr.trim();
        if (jsonStr.startsWith("{")) {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return jsonObject.toString(JSON_INDENT);
        }
        if (jsonStr.startsWith("[")) {
            JSONArray jsonArray = new JSONArray(jsonStr);
            return jsonArray.toString(JSON_INDENT);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonStr;
}

From source file:Main.java

public static final DateTime parseXmlDateTime(String dateTime) {
    if (dateTime == null || dateTime.trim().equals("")) {
        return null;
    }/*from  ww w . j  a  va  2 s.  c o m*/
    DateTimeParser[] parsers = new DateTimeParser[] {
            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").getParser(),
            ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed().getParser(),
            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").getParser(),
            ISODateTimeFormat.dateTime().withOffsetParsed().getParser(), };
    DateTimeFormatterBuilder dateTimeFormatterBuilder = new DateTimeFormatterBuilder();
    dateTimeFormatterBuilder.append(null, parsers);
    return dateTimeFormatterBuilder.toFormatter().parseDateTime(dateTime);
}