Here you can find the source of leftPadded(String src, int len)
Parameter | Description |
---|---|
src | a parameter |
len | a parameter |
public static String leftPadded(String src, int len)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w . j a v a2 s . co m*/ * Pad the src to make it [len] bytes with trailing spaces. * * @param src * @param len * @return */ public static String leftPadded(String src, int len) { return leftPadded(src, len, ' '); } /** * Pad the src to make it [len] bytes with trailing last parameter. * * @param src * @param len * @param padChar * @return */ public static String leftPadded(String src, int len, char padChar) { if (len < 1) return ""; if (src == null) src = ""; src = src.trim(); int srcLen = src.length(); if (srcLen > len) return src.substring(0, len); return iterateChar(padChar, len - srcLen) + src; } /** * Trims the character both from left and right * * @param str * @param filler * @return */ public static String trim(String str, final char filler) { // TODO: Performasi arttirilmali!! return rtrim(ltrim(str, filler), filler); } /** * Fills the string with the given char, as long as given length * * @param ch * @param len * @return */ public static String iterateChar(char ch, int len) { if (len < 1) return ""; char buf[] = new char[len]; for (int i = 0; i < len; i++) buf[i] = ch; return new String(buf); } /** * PErforms right trim * * @param str * @param filler * @return */ public static String rtrim(final String str, final char filler) { // TODO: performansi arttirilmali if (str == null) return null; for (int i = str.length() - 1; i >= 0; --i) { if (str.charAt(i) != filler) return str.substring(0, i + 1); } return ""; } /** * Performs left trim * * @param str * @param filler * @return */ public static String ltrim(final String str, final char filler) { // TODO: Performasi arttirilmali!! if (str == null) return null; for (int i = 0; i < str.length(); ++i) { if (str.charAt(i) != filler) { return str.substring(i); } } return ""; } }