Android examples for java.lang:String Pad
Gets the leftmost len characters of a String
/*//from w ww . j a v a 2 s. c o m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.text.Normalizer; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.regex.Pattern; public class Main{ /** * <p>Gets the leftmost {@code len} characters of a String.</p> * * <p>If {@code len} characters are not available, or the * String is {@code null}, the String will be returned without * an exception. An empty String is returned if len is negative.</p> * * <pre> * StringUtils.left(null, *) = null * StringUtils.left(*, -ve) = "" * StringUtils.left("", *) = "" * StringUtils.left("abc", 0) = "" * StringUtils.left("abc", 2) = "ab" * StringUtils.left("abc", 4) = "abc" * </pre> * * @param str the String to get the leftmost characters from, may be null * @param len the length of the required String * @return the leftmost characters, {@code null} if null String input */ public static String left(final String str, final int len) { if (str == null) { return null; } if (len < 0) { return ""; } if (str.length() <= len) { return str; } return str.substring(0, len); } }