0

I want to be able to replace from 8th character of string and on with dots in any string. How can this be done?

right now I have this:

if(tempName.length() > 10)
{
     name.setText(tempName.substring(0, 10));
} else {
     name.setText(tempName);
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
sys_debug
  • 3,883
  • 17
  • 67
  • 98

4 Answers4

4

If you want to replace substring after 8th character with an ellipsis, if string length is greater than 10, you can do it with single String#replaceAll. You don't even need to check for length before hand. Just use the below code:

A One Liner :

// No need to check for length before hand.
// It will only do a replace if length of str is greater than 10.
// Breaked into multiple lines for explanation
str = str.replaceAll(  "^"       // Match at the beginning
                     + "(.{7})"  // Capture 7 characters
                     + ".{4,}"   // Match 4 or more characters (length > 10)
                     + "$",      // Till the end.
                     "$1..."      
                    );     

Another option is of course a substring, which you already have in other answers.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • This is *not* what he wants. This code will replace *every* character after the 7th with a dot - leaving the same length, but he wants *all* characters after the 7th replaced with exactly 3 dots (an ellipsis) - leaving a length of exactly 10. The requirement is to limit the length to 10 always. – Bohemian Jul 27 '13 at 10:49
  • @Bohemian. Well, this is written nowhere in question that he wants just 3 dots. It's just that people assumed it. From his first line, I understand he wants to replace all character on and after 8th character to be replaced by `.` – Rohit Jain Jul 27 '13 at 10:50
  • He does say it. Look at his code. The maximum length is 10 for all cases. He current code doesn't add dots, but he wants to replace all characters from 8 on with dots - that means 3 dots. He's asking for help because his code *doesn't do it*. I'm going to have to -1 this. It's obvious what he is asking. He wants an [ellipsis](http://en.wikipedia.org/wiki/Ellipsis_(computer_programming)). – Bohemian Jul 27 '13 at 10:53
  • @Bohemian. I've asked for some input and output. Let's see what OP wants. I'll change my code accordingly. For now, it's not clear from his `text`, what he wants. – Rohit Jain Jul 27 '13 at 10:54
  • There's already an answer that is correct, and it seems very good - only 1 line. I will look forward to seeing if you can do better tho. Msg me if you code something better. – Bohemian Jul 27 '13 at 10:57
  • @Bohemian. I've added a solution, for ellipsis too. You can check onto that. – Rohit Jain Jul 27 '13 at 11:05
  • I already thought of that and decided it was stupidly complicated for no gain. It's more code and less readable: The -1 stays. The substring is the simplest and best approach. I tried to work out a way to do it with a single call to replaceAll without the if. If you can do that, that would be a worthy answer. – Bohemian Jul 27 '13 at 12:53
  • @Bohemian. That can be easily done. Will edit my answer. As for substring, yeah that's a better way, and I've already made a note of it in my answer. – Rohit Jain Jul 27 '13 at 12:56
  • @Bohemian. I've added the one-liner. Probably you can take a look. – Rohit Jain Jul 27 '13 at 13:49
  • Now we're talking! Why not delete that other rubbish and just leave the last solution, which is a worthy alternative. The rest isn't even worth reading. Quality, not quantity :) – Bohemian Jul 27 '13 at 13:57
  • @Bohemian. Was only doing that. :) – Rohit Jain Jul 27 '13 at 14:04
  • Yes I want only 3 dots and not all characters – sys_debug Jul 27 '13 at 14:28
  • @sys_debug. Then you can use the first one. I'll remove the 2nd part. – Rohit Jain Jul 27 '13 at 14:29
  • thanks mate for taking it out. Nevertheless, the marked answer has a much more organized way (i.e. a method and variables) than this. May the best always be selected :) – sys_debug Jul 27 '13 at 14:35
4
   public static String ellipsize(String input, int maxLength) {
      if (input == null || input.length() <= maxLength) {
        return input;
      }
      return input.substring(0, maxLength-3) + "...";
    }

This method will give string out put with max length maxLength. replacing all char after MaxLength-3 with ...

eg. maxLength=10

abc --> abc

1234567890 --> 1234567890

12345678901 --> 1234567...

swapy
  • 1,616
  • 1
  • 15
  • 31
3

Trying to replace with just three dots? Try this:

String original = "abcdefghijklmn";

String newOne = (original.length() > 10)? original.substring(0, 7) + "...": original;

Ternary operator ( A ? B : C ) does this: A is a boolean, if true, then evaluates to B, elsewhere evaluates to C. It can save you if statements every now and then.

morgano
  • 17,210
  • 10
  • 45
  • 56
0

A couple ways.

1. substring() and Concatenation

// If > 8...
String dotted = src.substring(0, 8) + "...";
// Else...

or

String dotted = src.length() > 8 ? src.substring(0, 8) + "..." : src;

2. Regex

// If > 8...
String dotted = src.replaceAll("^(.{8}).+$", "$1...");
// Else...

or

String dotted = src.length() > 8 ? src.replaceAll("^(.{8}).+$", "$1...") : src;
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145