Here you can find the source of subStrIfNeed(final String str, int num)
public static String subStrIfNeed(final String str, int num)
//package com.java2s; //License from project: Apache License public class Main { public static String subStrIfNeed(final String str, int num) { if (strIsNullOrEmpty(str)) { return str; }//from w w w.j av a2 s .c o m int len = str.length(); if (len <= num) { return str; } return str.substring(0, num); } /** * Judge string is null or empty. * * @param str str * @return boolean */ public static boolean strIsNullOrEmpty(final String str) { if (str == null || "".equals(str)) { return true; } return false; } }