Java String Sub String substring(String source, int length, String padding)

Here you can find the source of substring(String source, int length, String padding)

Description

substring

License

Open Source License

Parameter

Parameter Description
source a parameter
length a parameter
padding a parameter

Return

String

Declaration

public static String substring(String source, int length, String padding) 

Method Source Code

//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();
    }
}

Related

  1. substring(String s, int start, int length)
  2. substring(String s, int start, int stop)
  3. subString(String s, int startIndex, int count)
  4. substring(String self, Integer startIndex, Integer endIndex)
  5. substring(String source, int beginIndex, int endIndex)
  6. substring(String source, int offset, int dataLength)
  7. substring(String source, int size)
  8. substring(String source, int startIndex)
  9. subString(String source, int startIndex, int count)