I am using StringUtils.abbreviateMiddle(string, middle, targetLength);
to abbreviate string in the middle but using this method cuts off the word. Is there a way such that it will only cut off string at the space nearest to the middle position of the string?
public static String shortenStringMiddle(String string, String middle, int targetLength) {
targetLength = Math.abs(targetLength);
if (string != null && string.length() > targetLength) {
return StringUtils.abbreviateMiddle(string, middle, targetLength);
}
else {
return string;
}
}
Input if applied to this text:
Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques: Proceedings of the SEAFDEC-OIE Seminar-Workshop on Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques, 4-6 December 2001, Iloilo City, Philippines
System.out.println(StringUtils.abbreviateMiddle("Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques: Proceedings of the SEAFDEC-OIE Seminar-Workshop on Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques, 4-6 December 2001, Iloilo City, Philippines", " … ", 220));
Output:
Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques: Procee … aculture in Southeast Asia - Diagnosis and Husbandry Techniques, 4-6 December 2001, Iloilo City, Philippines
The word Proceedings was cut off to Procee and Aquaculture to aculture
My ideal output would be:
Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques: Proceedings … Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques, 4-6 December 2001, Iloilo City, Philippines
I've searched here in SO and similar questions only relates to abbreviate string adding an ellipsis at the end.