Java String Left left(String s, int len)

Here you can find the source of left(String s, int len)

Description

Gets the leftmost n characters of a string.

License

Open Source License

Parameter

Parameter Description
s The string to get the leftmost characters from
len The length of the required string

Return

The leftmost characters

Declaration

public static String left(String s, int len) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w  w w  .  ja  v a 2s.  co  m*/
     * Gets the leftmost n characters of a string. If n characters are not
     * available, or the string is null, the string will be returned without an
     * exception.
     * 
     * @param s
     *            The string to get the leftmost characters from
     * @param len
     *            The length of the required string
     * @return The leftmost characters
     */
    public static String left(String s, int len) {
        if (len < 0)
            throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
        return ((s == null) || (s.length() <= len)) ? s : s.substring(0, len);
    }
}

Related

  1. left(final String string, final int length)
  2. left(final String text, final String sep)
  3. left(Object src, int length, String defaultValue)
  4. left(String baseString, int pos)
  5. left(String s, int l)
  6. left(String s, int nCaratteri)
  7. left(String s, int size)
  8. left(String s, int width)
  9. left(String source, int length)