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

/**
 * Makes a given filename safe by replacing special characters like colons (":")
 * with dashes ("-").//from ww  w  . j av a 2  s .c o m
 *
 * @param path The path of the directory in question.
 * @return The the directory name with special characters replaced by hyphens.
 */
private static String fileSystemSafeDir(String path) {
    if (path == null || path.trim().length() == 0) {
        return "";
    }

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

From source file:Main.java

public static boolean isPhoneNumberValid(String phoneNumber) {
    if ((phoneNumber == null) || (phoneNumber.trim().equals(""))) {
        return false;
    }/*from w w  w . ja  va  2 s .c om*/
    boolean isValid = false;
    String expression_r_r = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
    Pattern pattern = Pattern.compile(expression_r_r);
    Matcher matcher = pattern.matcher(phoneNumber);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}

From source file:Main.java

public static boolean isEmpty(String str) {
    if (str == null || str.trim().equals("") || str.trim().equals("null"))
        return true;
    return false;
}

From source file:Main.java

/**
 * Modify the given String so that it can be used as part of a filename
 * without causing problems from illegal/special characters.
 *
 * The result should be similar to the input, but isn't necessarily
 * reversible.//from w  w  w.  j  a va  2  s  . c  o m
 */
public static String replaceBadChars(String name) {
    if (name == null || name.trim().length() == 0) {
        return "_";
    }
    name = name.trim();
    for (char badChar : badFilenameChars) {
        name = name.replace(badChar, '_');
    }
    return name;
}

From source file:Main.java

public static String trimToNull(String nameDirty) {
    String name = nameDirty;
    if (name != null) {
        name = name.trim();
        if (name.length() < 1) {
            name = null;/*  ww  w.  j a v a 2s.  c  om*/
        }
    }
    return name;
}

From source file:Main.java

/**
 * Node to string./*  w  ww  . j  a v  a2s  .  com*/
 *
 * @param node
 *            the node
 * @return the string
 */
public static String nodeToString(Node node) {
    Document doc = node.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) doc.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", false);
    String string = serializer.writeToString(node);
    return string.trim();
}

From source file:Main.java

/**
 * convert date string with format yyyy-MM-dd to millisecond
 *
 * @param date//from w  w  w . ja v a  2 s.c  om
 * @return -1 if exception throw
 */
public static long convertDateStr2Millis(String date) {
    if (date == null || date.trim().length() == 0)
        return -1;
    try {
        return formatYYYY_MM_DD.parse(date).getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:com.mboarder.string.TextViewString.java

public static String RemoveHtmlMarker(String InStirng) {
    return StringEscapeUtils.unescapeHtml(InStirng.trim());
}

From source file:Main.java

public static void cleanWhiteSpaceNodes(Element node, boolean deep) {
    NodeList list = node.getChildNodes();
    ArrayList temp = new ArrayList();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);//from w w w.java 2  s.com
        short type = n.getNodeType();
        if (type == 1) {
            Element e = (Element) n;
            cleanWhiteSpaceNodes(e, deep);
        } else if (type == 3) {
            Text text = (Text) n;
            String val = text.getData();
            if (val.trim().equals(""))
                temp.add(text);
        }
    }

    for (Iterator i = temp.iterator(); i.hasNext(); node.removeChild((Node) i.next()))
        ;
}

From source file:Main.java

/**
 * This method accepts an XML date string and will return a JodaTime object.
 * It will need to be updated to support timezones.
 * /* www  . ja va2s .co m*/
 * @param date
 * @return
 */
public static final DateTime parseXmlDate(String date) {
    if (date == null || date.trim().equals("")) {
        return null;
    }
    DateTimeParser[] parsers = new DateTimeParser[] { DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
            ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed().getParser(), };
    DateTimeFormatterBuilder dateTimeFormatterBuilder = new DateTimeFormatterBuilder();
    dateTimeFormatterBuilder.append(null, parsers);
    return dateTimeFormatterBuilder.toFormatter().parseDateTime(date);
}