Java Code Examples for java.lang.String#length()

The following examples show how to use java.lang.String#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Messages.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the message text corresponding to the passed msgkey
 * string.  The message text is assumed to require the insertion
 * of two arguments, supplied by the "parm1" and "parm2" parameters.
 * If the message text does not contain the meta characters "%1" and
 * "%2" that indicate where to place the arguments, the passed arguments
 * are appended at the end of the message text.
 * <p>
 * If the msgkey cannot be found, its value is used as the
 * message text.
 */
public static final String msg (String msgkey, String parm1, String parm2) {

    if (loadNeeded)
        loadDefaultProperties ();
    String result = m.getProperty (msgkey, msgkey);
    String ending = "";
    int i = result.indexOf ("%1");
    if (i >= 0) {
        if ((i+2) < result.length ())
            ending = result.substring (i+2);
        result = result.substring (0, i) + parm1 + ending;
    } else
        result += " " + parm1;
    i = result.indexOf ("%2");
    if (i >= 0) {
        ending = "";
        if ((i+2) < result.length ())
            ending = result.substring (i+2);
        result = result.substring (0, i) + parm2 + ending;
    } else
        result += " " + parm2;
    return result;

}
 
Example 2
Source File: Messages.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static String substituteString(String orig, int paramNum,
                 String subst){
    String result = orig;
    String paramSubst = "%"+ paramNum;
    int len = paramSubst.length();
    int index = result.indexOf (paramSubst);
    String ending = "";
    if (index >= 0) {
        if ((index+len) < result.length ())
            ending = result.substring (index+len);
        result = result.substring (0, index) + subst + ending;
    }
    else result += " " + subst;

     return result;
}
 
Example 3
Source File: Messages.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static String substituteString(String orig, int paramNum,
                 String subst){
    String result = orig;
    String paramSubst = "%"+ paramNum;
    int len = paramSubst.length();
    int index = result.indexOf (paramSubst);
    String ending = "";
    if (index >= 0) {
        if ((index+len) < result.length ())
            ending = result.substring (index+len);
        result = result.substring (0, index) + subst + ending;
    }
    else result += " " + subst;

     return result;
}
 
Example 4
Source File: Messages.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the message text corresponding to the passed msgkey
 * string.  The message text is assumed to require the insertion
 * of two arguments, supplied by the "parm1" and "parm2" parameters.
 * If the message text does not contain the meta characters "%1" and
 * "%2" that indicate where to place the arguments, the passed arguments
 * are appended at the end of the message text.
 * <p>
 * If the msgkey cannot be found, its value is used as the
 * message text.
 */
public static final String msg (String msgkey, String parm1, String parm2) {

    if (loadNeeded)
        loadDefaultProperties ();
    String result = m.getProperty (msgkey, msgkey);
    String ending = "";
    int i = result.indexOf ("%1");
    if (i >= 0) {
        if ((i+2) < result.length ())
            ending = result.substring (i+2);
        result = result.substring (0, i) + parm1 + ending;
    } else
        result += " " + parm1;
    i = result.indexOf ("%2");
    if (i >= 0) {
        ending = "";
        if ((i+2) < result.length ())
            ending = result.substring (i+2);
        result = result.substring (0, i) + parm2 + ending;
    } else
        result += " " + parm2;
    return result;

}
 
Example 5
Source File: Messages.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the message text corresponding to the passed msgkey
 * string.  The message text is assumed to require the insertion
 * of a single argument, supplied by the "parm" parameter.
 * If the message text does not contain the meta characters "%1"
 * that indicate where to place the argument, the passed argument
 * is appended at the end of the message text.
 * <p>
 * If the msgkey cannot be found, its value is used as the
 * message text.
 */
public static final String msg (String msgkey, String parm) {

    if (loadNeeded)
        loadDefaultProperties ();
    String msgtext = m.getProperty (msgkey, msgkey);
    int i = msgtext.indexOf ("%1");
    if (i >= 0) {
        String ending = "";
        if ((i+2) < msgtext.length ())
            ending = msgtext.substring (i+2);
        return msgtext.substring (0, i) + parm + ending;
    } else
        msgtext += " " + parm;
    return msgtext;

}
 
Example 6
Source File: Message.java    From secor with Apache License 2.0 6 votes vote down vote up
/**
 * Message key and payload may be arbitrary binary strings, so we should make sure we don't throw
 * when logging them by using a CharsetDecoder which replaces bad data. (While in practice `new String(bytes)`
 * does the same thing, the documentation for that method leaves that behavior unspecified.)
 * Additionally, in contexts where Message.toString() will be logged at a high level (including exception
 * messages), we truncate long keys and payloads, which may be very long binary data.
 */
private String bytesToString(byte[] bytes, boolean truncate) {
    CharsetDecoder decoder = Charset.defaultCharset()
            .newDecoder()
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    CharBuffer charBuffer;
    try {
        charBuffer = decoder.decode(byteBuffer);
    } catch (CharacterCodingException e) {
        // Shouldn't happen due to choosing REPLACE above, but Java makes us catch it anyway.
        throw new RuntimeException(e);
    }
    String s = charBuffer.toString();
    if (truncate && s.length() > TRUNCATED_STRING_MAX_LEN) {
        return new StringBuilder().append(s, 0, TRUNCATED_STRING_MAX_LEN).append("[...]").toString();
    } else {
        return s;
    }
}
 
Example 7
Source File: Messages.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static String substituteString(String orig, int paramNum,
                 String subst){
    String result = orig;
    String paramSubst = "%"+ paramNum;
    int len = paramSubst.length();
    int index = result.indexOf (paramSubst);
    String ending = "";
    if (index >= 0) {
        if ((index+len) < result.length ())
            ending = result.substring (index+len);
        result = result.substring (0, index) + subst + ending;
    }
    else result += " " + subst;

     return result;
}
 
Example 8
Source File: Messages.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the message text corresponding to the passed msgkey
 * string.  The message text is assumed to require the insertion
 * of a single argument, supplied by the "parm" parameter.
 * If the message text does not contain the meta characters "%1"
 * that indicate where to place the argument, the passed argument
 * is appended at the end of the message text.
 * <p>
 * If the msgkey cannot be found, its value is used as the
 * message text.
 */
public static final String msg (String msgkey, String parm) {

    if (loadNeeded)
        loadDefaultProperties ();
    String msgtext = m.getProperty (msgkey, msgkey);
    int i = msgtext.indexOf ("%1");
    if (i >= 0) {
        String ending = "";
        if ((i+2) < msgtext.length ())
            ending = msgtext.substring (i+2);
        return msgtext.substring (0, i) + parm + ending;
    } else
        msgtext += " " + parm;
    return msgtext;

}
 
Example 9
Source File: PathList.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method for appending path from pathFrom to pathTo.
 *
 * @param pathTo the target path
 * @param pathSource the path to be appended to pathTo
 * @return the resulting path
 */
public static String appendPath(String pathTo, String pathFrom) {
    if (pathTo == null || pathTo.length() == 0) {
        return pathFrom;
    } else if (pathFrom == null || pathFrom.length() == 0) {
        return pathTo;
    } else {
        return pathTo  + File.pathSeparator + pathFrom;
    }
}
 
Example 10
Source File: PathList.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method for appending path from pathFrom to pathTo.
 *
 * @param pathTo the target path
 * @param pathSource the path to be appended to pathTo
 * @return the resulting path
 */
public static String appendPath(String pathTo, String pathFrom) {
    if (pathTo == null || pathTo.length() == 0) {
        return pathFrom;
    } else if (pathFrom == null || pathFrom.length() == 0) {
        return pathTo;
    } else {
        return pathTo  + File.pathSeparator + pathFrom;
    }
}
 
Example 11
Source File: JarBundler.java    From JarBundler with Apache License 2.0 5 votes vote down vote up
public void addConfiguredService(Service service) {

		//if (service.getPortName() == null)
		//	throw new BuildException("\"<service>\" must have a \"portName\" attribute");
		
		if (service.getMessage() == null)
			throw new BuildException("\"<service>\" must have a \"message\" attribute");
		
		String menuItem = service.getMenuItem();
		if (menuItem == null)
			throw new BuildException("\"<service>\" must have a \"menuItem\" attribute");
		if (!menuItems.add(menuItem))
			throw new BuildException("\"<service>\" \"menuItem\" value must be unique");
		
		if (service.getSendTypes().isEmpty() && service.getReturnTypes().isEmpty())
			throw new BuildException("\"<service>\" must have either a \"sendTypes\" attribute, a \"returnTypes\" attribute or both");
		
		String keyEquivalent = service.getKeyEquivalent();
		if ((keyEquivalent != null) && (1 != keyEquivalent.length()))
			throw new BuildException("\"<service>\" \"keyEquivalent\" must be one character if present");
		
		String timeoutString = service.getTimeout();
		if (timeoutString != null) {
			long timeout = -1;
			try {
				timeout = Long.parseLong(timeoutString);
			} catch (NumberFormatException nfe) {
				throw new BuildException("\"<service>\" \"timeout\" must be a positive integral number");
			}
			if (timeout < 0)
				throw new BuildException("\"<service>\" \"timeout\" must not be negative");
		}
		
		bundleProperties.addService(service);
	}
 
Example 12
Source File: PathList.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method for appending path from pathFrom to pathTo.
 *
 * @param pathTo the target path
 * @param pathFrom the path to be appended to pathTo
 * @return the resulting path
 */
public static String appendPath(String pathTo, String pathFrom) {
    if (pathTo == null || pathTo.length() == 0) {
        return pathFrom;
    } else if (pathFrom == null || pathFrom.length() == 0) {
        return pathTo;
    } else {
        return pathTo  + File.pathSeparator + pathFrom;
    }
}
 
Example 13
Source File: AppBundleProperties.java    From JarBundler with Apache License 2.0 5 votes vote down vote up
public void setCFBundleName(String s) {

		if (s.length() > 16)
			System.err
					.println("WARNING: 'shortname' is recommeded to be no more than 16 "
							+ "charaters long. See usage notes.");
		mCFBundleName = s;
	}
 
Example 14
Source File: Messages.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method was introduced to fix defect #26964.  For Java 1.0.2
 * on Win NT, the escape sequence \u0020 was not being handled
 * correctly by the Java Properties class when it was the final
 * character of a line.  Instead the trailing blank was dropped
 * and the next line was swallowed as a continuation.  To work
 * around the problem, we introduced our own metasymbol to represent
 * a trailing blank.  Hence:
 *
 * Performs substitution for any metasymbols in the message
 * templates.  So far only %B is needed.  This was introduced
 * to make it more convenient for .properties files to
 * contain message templates with leading or trailing blanks
 * (although %B may actually occur anywhere in a template).
 * Subsequently, checking for '\n' has also been added.  Now,
 * wherever '\n' occurs in a message template, it is replaced
 * with the value of System.getProperty ("line.separator").
 */
private static final void fixMessages (Properties p) {

    Enumeration keys = p.keys ();
    Enumeration elems = p.elements ();
    while (keys.hasMoreElements ()) {
        String key = (String) keys.nextElement ();
        String elem = (String) elems.nextElement ();
        int i = elem.indexOf (LTB);
        boolean changed = false;
        while (i != -1) {
            if (i == 0)
                elem = " " + elem.substring (2);
            else
                elem = elem.substring (0, i) + " " + elem.substring (i+2);
            changed = true;
            i = elem.indexOf (LTB);
        }
        int lsIncr = lineSeparator.length () - 1;
        for (i=0; i<elem.length (); i++) {
            if (elem.charAt (i) == NL) {
                elem = elem.substring (0, i) +
                    lineSeparator + elem.substring (i+1);
                i += lsIncr;
                changed = true;
            }
        }
        if (changed)
            p.put (key, elem);
    }

}
 
Example 15
Source File: PathList.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method for appending path from pathFrom to pathTo.
 *
 * @param pathTo the target path
 * @param pathSource the path to be appended to pathTo
 * @return the resulting path
 */
public static String appendPath(String pathTo, String pathFrom) {
    if (pathTo == null || pathTo.length() == 0) {
        return pathFrom;
    } else if (pathFrom == null || pathFrom.length() == 0) {
        return pathTo;
    } else {
        return pathTo  + File.pathSeparator + pathFrom;
    }
}
 
Example 16
Source File: Messages.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method was introduced to fix defect #26964.  For Java 1.0.2
 * on Win NT, the escape sequence \u0020 was not being handled
 * correctly by the Java Properties class when it was the final
 * character of a line.  Instead the trailing blank was dropped
 * and the next line was swallowed as a continuation.  To work
 * around the problem, we introduced our own metasymbol to represent
 * a trailing blank.  Hence:
 *
 * Performs substitution for any metasymbols in the message
 * templates.  So far only %B is needed.  This was introduced
 * to make it more convenient for .properties files to
 * contain message templates with leading or trailing blanks
 * (although %B may actually occur anywhere in a template).
 * Subsequently, checking for '\n' has also been added.  Now,
 * wherever '\n' occurs in a message template, it is replaced
 * with the value of System.getProperty ("line.separator").
 */
private static final void fixMessages (Properties p) {

    Enumeration keys = p.keys ();
    Enumeration elems = p.elements ();
    while (keys.hasMoreElements ()) {
        String key = (String) keys.nextElement ();
        String elem = (String) elems.nextElement ();
        int i = elem.indexOf (LTB);
        boolean changed = false;
        while (i != -1) {
            if (i == 0)
                elem = " " + elem.substring (2);
            else
                elem = elem.substring (0, i) + " " + elem.substring (i+2);
            changed = true;
            i = elem.indexOf (LTB);
        }
        int lsIncr = lineSeparator.length () - 1;
        for (i=0; i<elem.length (); i++) {
            if (elem.charAt (i) == NL) {
                elem = elem.substring (0, i) +
                    lineSeparator + elem.substring (i+1);
                i += lsIncr;
                changed = true;
            }
        }
        if (changed)
            p.put (key, elem);
    }

}
 
Example 17
Source File: Messages.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method was introduced to fix defect #26964.  For Java 1.0.2
 * on Win NT, the escape sequence \u0020 was not being handled
 * correctly by the Java Properties class when it was the final
 * character of a line.  Instead the trailing blank was dropped
 * and the next line was swallowed as a continuation.  To work
 * around the problem, we introduced our own metasymbol to represent
 * a trailing blank.  Hence:
 *
 * Performs substitution for any metasymbols in the message
 * templates.  So far only %B is needed.  This was introduced
 * to make it more convenient for .properties files to
 * contain message templates with leading or trailing blanks
 * (although %B may actually occur anywhere in a template).
 * Subsequently, checking for '\n' has also been added.  Now,
 * wherever '\n' occurs in a message template, it is replaced
 * with the value of System.getProperty ("line.separator").
 */
private static final void fixMessages (Properties p) {

    Enumeration keys = p.keys ();
    Enumeration elems = p.elements ();
    while (keys.hasMoreElements ()) {
        String key = (String) keys.nextElement ();
        String elem = (String) elems.nextElement ();
        int i = elem.indexOf (LTB);
        boolean changed = false;
        while (i != -1) {
            if (i == 0)
                elem = " " + elem.substring (2);
            else
                elem = elem.substring (0, i) + " " + elem.substring (i+2);
            changed = true;
            i = elem.indexOf (LTB);
        }
        int lsIncr = lineSeparator.length () - 1;
        for (i=0; i<elem.length (); i++) {
            if (elem.charAt (i) == NL) {
                elem = elem.substring (0, i) +
                    lineSeparator + elem.substring (i+1);
                i += lsIncr;
                changed = true;
            }
        }
        if (changed)
            p.put (key, elem);
    }

}
 
Example 18
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Checks whether the <code>String</code> contains only
 * digit characters.</p>
 *
 * <p><code>Null</code> and empty String will return
 * <code>false</code>.</p>
 *
 * @param str  the <code>String</code> to check
 * @return <code>true</code> if str contains only Unicode numeric
 */
public static boolean isDigits(final String str) {
    if (TextUtils.isEmpty(str)) {
        return false;
    }
    for (int i = 0; i < str.length(); i++) {
        if (!Character.isDigit(str.charAt(i))) {
            return false;
        }
    }
    return true;
}
 
Example 19
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public static List<String> lexSqlScript(String sqlScript) {
	ArrayList<String> sl = new ArrayList<String>();
	boolean inString = false, quoteNext = false;
	StringBuilder b = new StringBuilder(100);

	for (int i = 0; i < sqlScript.length(); i++) {
		char c = sqlScript.charAt(i);

		if (c == ';' && !inString && !quoteNext) {
			sl.add(b.toString());
			b = new StringBuilder(100);
			inString = false;
			quoteNext = false;
			continue;
		}

		if (c == '\'' && !quoteNext) {
			inString = !inString;
		}

		quoteNext = c == '\\' && !quoteNext;

		b.append(c);
	}

	if (b.length() > 0) {
		sl.add(b.toString());
	}

	return sl;
}
 
Example 20
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Utility method for {@link #createNumber(String)}.</p>
 *
 * <p>Returns <code>true</code> if s is <code>null</code>.</p>
 *
 * @param str  the String to check
 * @return if it is all zeros or <code>null</code>
 */
private static boolean isAllZeros(final String str) {
    if (str == null) {
        return true;
    }
    for (int i = str.length() - 1; i >= 0; i--) {
        if (str.charAt(i) != '0') {
            return false;
        }
    }
    return str.length() > 0;
}