Here you can find the source of substring(String source, int length, String padding)
Parameter | Description |
---|---|
source | a parameter |
length | a parameter |
padding | a parameter |
public static String substring(String source, int length, String padding)
//package com.java2s; /*//from ww w .j a v a2 s. c o m * $RCSfile: StringUtil.java,v $$ * $Revision: 1.1 $ * $Date: 2013-03-02 $ * * Copyright (C) 2008 Skin, Inc. All rights reserved. * * This software is the proprietary information of Skin, Inc. * Use is subject to license terms. */ public class Main { /** * @param source * @param length * @param padding * @return String */ public static String substring(String source, int length, String padding) { if (source == null) { return ""; } String s = source.trim(); char c; int size = 0; int count = s.length(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < s.length(); i++) { c = s.charAt(i); if (c >= 0x0080) { size += 2; count++; } else { size++; } if (size > length) { if (c >= 0x4e00) { size -= 2; } else { size--; } break; } buffer.append(c); } if (size < count && padding != null) { buffer.append(padding); } return buffer.toString(); } }