I Spy Code - Java

Abbreviate middle

Question:

Write a java program that abbreviates a String, replacing the middle characters.

Solution:

Here is a java example that abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
   public static void main(String[] args) {
 
      String str1 = "supercalifragilisticexpialidocious";
      System.out.println(str1);
 
      String str2 = StringUtils.abbreviateMiddle(str1, "...", 10);
      System.out.println(str2);
 
   }
}
 

Output:

$ java Example
supercalifragilisticexpialidocious
supe...ous

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#abbreviateMiddle-java.lang.String-java.lang.String-int-

Questions answered by this page:

Java example that string truncate middle with ellipsis