Java String Sub String subString(String str, int num, String token)

Here you can find the source of subString(String str, int num, String token)

Description

sub String

License

Apache License

Declaration

public static String subString(String str, int num, String token) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String subString(String str, int num, String token) {
        String str1 = str;//w  w  w .j av a  2 s  . c  o m
        if (str != null && str.trim().length() > num) {
            str1 = str.substring(0, num);
        }
        if (token != null) {
            str1 = str1 + token;
        }
        return str1;
    }

    public static String subString(String str, int bytes) {
        int cutLength = 0;
        int byteNum = bytes;
        byte bt[] = str.getBytes();
        if (bytes > 1) {
            for (int i = 0; i < byteNum; i++) {
                if (bt[i] < 0) {
                    cutLength++;

                }
            }
            if (cutLength % 2 == 0) {
                cutLength /= 2;
            } else {
                cutLength = 0;
            }
        }
        int result = cutLength + --byteNum;
        if (result > bytes) {
            result = bytes;
        }
        if (bytes == 1) {
            if (bt[0] < 0) {
                result += 2;

            } else {
                result += 1;
            }
        }
        String substrx = new String(bt, 0, result);
        return substrx;
    }
}

Related

  1. substring(String str, int beginIndex, int len)
  2. subString(String str, int end)
  3. substring(String str, int index, int length)
  4. subString(String str, int len)
  5. substring(String str, int len)
  6. substring(String str, int off, int len)
  7. subString(String str, int offset, int leng)
  8. substring(String str, int start)
  9. substring(String str, int start)