用省略号截断字符串的理想方法 [英] Ideal method to truncate a string with ellipsis

查看:134
本文介绍了用省略号截断字符串的理想方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信我们所有人都在Facebook状态(或其他地方)看过省略号,然后点击显示更多并且只有另外2个字符左右。我猜这是因为懒惰的编程,因为肯定有一种理想的方法。

I'm sure all of us have seen ellipsis' on Facebook statuses (or elsewhere), and clicked "Show more" and there are only another 2 characters or so. I'd guess this is because of lazy programming, because surely there is an ideal method.

我的计算超薄字符 [iIl1] 作为半个字符,但是当它们几乎不隐藏任何字符时,这并没有让人看起来很傻。

Mine counts slim characters [iIl1] as "half characters", but this doesn't get around ellipsis' looking silly when they hide barely any characters.

有没有理想的方法?这是我的:

Is there an ideal method? Here is mine:

/**
 * Return a string with a maximum length of <code>length</code> characters.
 * If there are more than <code>length</code> characters, then string ends with an ellipsis ("...").
 *
 * @param text
 * @param length
 * @return
 */
public static String ellipsis(final String text, int length)
{
    // The letters [iIl1] are slim enough to only count as half a character.
    length += Math.ceil(text.replaceAll("[^iIl]", "").length() / 2.0d);

    if (text.length() > length)
    {
        return text.substring(0, length - 3) + "...";
    }

    return text;
}

语言并不重要,但标记为Java,因为这就是我的意思我最感兴趣的是看。

Language doesn't really matter, but tagged as Java because that's what I'm mostly interested in seeing.

推荐答案

我喜欢让瘦字符算作半个字符的想法。简单而且很好的近似。

I like the idea of letting "thin" characters count as half a character. Simple and a good approximation.

然而,大多数椭圆化的主要问题是(imho)他们在中间砍掉了。这是一个考虑词边界的解决方案(但不会涉及像素数学和Swing-API)。

The main issue with most ellipsizings however, are (imho) that they chop of words in the middle. Here is a solution taking word-boundaries into account (but does not dive into pixel-math and the Swing-API).

private final static String NON_THIN = "[^iIl1\\.,']";

private static int textWidth(String str) {
    return (int) (str.length() - str.replaceAll(NON_THIN, "").length() / 2);
}

public static String ellipsize(String text, int max) {

    if (textWidth(text) <= max)
        return text;

    // Start by chopping off at the word before max
    // This is an over-approximation due to thin-characters...
    int end = text.lastIndexOf(' ', max - 3);

    // Just one long word. Chop it off.
    if (end == -1)
        return text.substring(0, max-3) + "...";

    // Step forward as long as textWidth allows.
    int newEnd = end;
    do {
        end = newEnd;
        newEnd = text.indexOf(' ', end + 1);

        // No more spaces.
        if (newEnd == -1)
            newEnd = text.length();

    } while (textWidth(text.substring(0, newEnd) + "...") < max);

    return text.substring(0, end) + "...";
}

算法测试如下:

这篇关于用省略号截断字符串的理想方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆