Empleador activo
Implement a function to return the first n chars of a text, without breaking any word.
Anónimo
private static String smartSubString() { String str = "This is the test string which requires to be split in without loosing word"; int subStringIndex = 16; String finalString = ""; if(str.length() >= subStringIndex) { if (str.charAt(subStringIndex) == ' ') { finalString = str.substring(0, subStringIndex); } else { String nextStr = str.substring(0, subStringIndex); int indexOfSpaceFromEnd = nextStr.lastIndexOf(' '); finalString = nextStr.substring(0, indexOfSpaceFromEnd); } } return finalString; }