Java String Left left(String str, int count)

Here you can find the source of left(String str, int count)

Description

Returns the first n characters from this string, where n = count.

License

Apache License

Parameter

Parameter Description
str The string to parse.
count The amount of characters to return.

Return

The first n characters from this string, where n = count.

Declaration

public static String left(String str, int count) 

Method Source Code

//package com.java2s;
/*/*from w  w w .  jav a2 s  . com*/
 * Copyright 2010-2017 Boxfuse GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Returns the first n characters from this string, where n = count. If the string is shorter, the entire string
     * will be returned. If the string is longer, it will be truncated.
     *
     * @param str   The string to parse.
     * @param count The amount of characters to return.
     * @return The first n characters from this string, where n = count.
     */
    public static String left(String str, int count) {
        if (str == null) {
            return null;
        }

        if (str.length() < count) {
            return str;
        }

        return str.substring(0, count);
    }
}

Related

  1. left(String s, int size)
  2. left(String s, int width)
  3. left(String source, int length)
  4. left(String source, String searchFor)
  5. left(String source, String searchFor)
  6. left(String str, int len)
  7. left(String str, int len)
  8. left(String str, int len)
  9. left(String str, int len)