Here you can find the source of subString(String str, int num, String token)
public static String subString(String str, int num, String token)
//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; } }