2

If I have a String:

String neatish = getTheString(); // returns "...neat..."

I know that I can get rid of the first ellipsis using:

getTheString().substring(3);

But I'm wondering if there's a similar one-line method that takes the end off, based on length? Like:

getTheString().substring(3).truncate(3);

I don't want a character-specific method, ie. one that works only on ellipses.

While there's a substring() that accepts two parameters, it requires the altering String to be saved off to a variable first to determine the length. (Or, you can call getTheString() twice.)

I can certainly write one, but I'm wondering if there is a one-line method either in the standard java.lang package, or in Apache Commons-Lang, that will accomplish this.

I'm 50% curious, and 50% avoiding re-inventing the wheel.

Update: To avoid confusion, I do not want:

String neatish = getTheString();
neatish = neatish.substring(3)...;

I'm instead looking for the back-end version of substring(), and wondering why there isn't one.

11
  • the length of "somestring".substring(3) is usually "somestring".length() - 3 Commented Feb 12, 2014 at 18:21
  • Am I missing something or don't you just need neatish = neatish.replaceAll("\\.\\.\\.", ""); ? Commented Feb 12, 2014 at 18:21
  • @BrianRoach I don't want an ellipsis-specific pattern, just a general method based on a length. Commented Feb 12, 2014 at 18:23
  • 1
    @CraigOtis So ... I may still be being dense, but wouldn't the two-arg version of substring() do what you want? Commented Feb 12, 2014 at 18:32
  • 1
    @BrianRoach What do you propose the second parameter of his two arg substring be if the location of the ellipsis is unknown? Commented Feb 12, 2014 at 18:33

4 Answers 4

2

Fun exercise

String theString = new StringBuilder(getTheString()).delete(0, 3).reverse().delete(0, 3).reverse().toString();

Get the String into a StringBuilder, remove the first 3 chars, reverse it, remove the last 3 chars (which are now at the start), reverse it again.

Sign up to request clarification or add additional context in comments.

1 Comment

I think K.C. has you beat. :) But this is a pretty creative answer.
1

You can use subStringBefore and subStringAfter from Commons:

StringUtils.subStringAfter(StringUtils.subStringBefore(getTheString(), "..."), "...");

EDIT based on length:

You need: StringUtils.html#substring(java.lang.String, int, int)

StringUtils.substring(getTheString(), 3, -3);

3 Comments

Not looking for an ellipsis-specific method though - just one based on length.
Your updated answer with the negative third parameter is the best so far. I didn't realize that the StringUtils version of substring() accepted negative arguments.
Apache Commons has eeeeverything ;)
0
getTheString().substring(3).replaceFirst("(?s)...$", "");
getTheString().replaceFirst("(?s)^...(.*)...$", "$1");

This removes the last 3 chars by a regular expression, where (?s) make . also match newlines. Use \\. to match a period. UGLY.

Comments

0

You could write a method to do it.

public static String trimChar(String s, char ch) {
    int start = 0;
    for(; start < s.length(); start++)
         if (s.charAt(start) != ch)
             break;
    int end = s.length();
    for(; end > start; end--)
         if (s.charAt(end-1) != ch)
             break;
    return s.substring(start, end);
}


String s = trimChar(getTheString(), '.');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.