1

Say I have an unknown string of 100+ characters and I want to remove the last characters before the 100th character and replace them with ..., deleting every character after the 100th. ie:

I would like to remove the last three characters before the 100th character and replace them with a period and deleting everything after.

would become:

I would like to remove the last three characters before the 100th character and replace them with....

What is the best way to tackle this problem?

1 Answers1

1

Let String original = ... denote the first text. Then I suspect the following String is what you seek:

String result = String.format("%s%s", original.substring(0, Math.min(100, original.length())), original.length() > 100 ? "..." : "")
mstaal
  • 590
  • 9
  • 25