org.apache.commons.lang3.StringUtils.abbreviate()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(14.7k)|赞(0)|评价(0)|浏览(130)

本文整理了Java中org.apache.commons.lang3.StringUtils.abbreviate()方法的一些代码示例,展示了StringUtils.abbreviate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringUtils.abbreviate()方法的具体详情如下:
包路径:org.apache.commons.lang3.StringUtils
类名称:StringUtils
方法名:abbreviate

StringUtils.abbreviate介绍

[英]Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."

Specifically:

  • If the number of characters in str is less than or equal to maxWidth, return str.
  • Else abbreviate it to (substring(str, 0, max-3) + "...").
  • If maxWidth is less than 4, throw an IllegalArgumentException.
  • In no case will it return a String of length greater than maxWidth.
StringUtils.abbreviate(null, *)      = null 
StringUtils.abbreviate("", 4)        = "" 
StringUtils.abbreviate("abcdefg", 6) = "abc..." 
StringUtils.abbreviate("abcdefg", 7) = "abcdefg" 
StringUtils.abbreviate("abcdefg", 8) = "abcdefg" 
StringUtils.abbreviate("abcdefg", 4) = "a..." 
StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException

[中]使用省略号缩写字符串。这将把“现在是所有好人的时候”变成“现在是……的时候了”
明确地:
*如果str中的字符数小于或等于maxWidth,则返回str。
*否则将其缩写为(子字符串(str,0,max-3)+“…”。
*如果maxWidth小于4,则抛出IllegalArgumentException。
*在任何情况下,它都不会返回长度大于maxWidth的字符串。

StringUtils.abbreviate(null, *)      = null 
StringUtils.abbreviate("", 4)        = "" 
StringUtils.abbreviate("abcdefg", 6) = "abc..." 
StringUtils.abbreviate("abcdefg", 7) = "abcdefg" 
StringUtils.abbreviate("abcdefg", 8) = "abcdefg" 
StringUtils.abbreviate("abcdefg", 4) = "a..." 
StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException

代码示例

代码示例来源:origin: Activiti/Activiti

public void setExceptionMessage(String exceptionMessage) {
 this.exceptionMessage = StringUtils.abbreviate(exceptionMessage, MAX_EXCEPTION_MESSAGE_LENGTH);
}

代码示例来源:origin: org.apache.commons/commons-lang3

return abbreviate(str, defaultAbbrevMarker, 0, maxWidth);

代码示例来源:origin: org.apache.commons/commons-lang3

return abbreviate(str, defaultAbbrevMarker, offset, maxWidth);

代码示例来源:origin: org.apache.commons/commons-lang3

return abbreviate(str, abbrevMarker, 0, maxWidth);

代码示例来源:origin: Activiti/Activiti

@Override
public String toString() {
 StringBuilder sb = new StringBuilder();
 sb.append("HistoricDetailVariableInstanceUpdateEntity[");
 sb.append("id=").append(id);
 sb.append(", name=").append(name);
 sb.append(", type=").append(variableType != null ? variableType.getTypeName() : "null");
 if (longValue != null) {
  sb.append(", longValue=").append(longValue);
 }
 if (doubleValue != null) {
  sb.append(", doubleValue=").append(doubleValue);
 }
 if (textValue != null) {
  sb.append(", textValue=").append(StringUtils.abbreviate(textValue, 40));
 }
 if (textValue2 != null) {
  sb.append(", textValue2=").append(StringUtils.abbreviate(textValue2, 40));
 }
 if (byteArrayRef != null && byteArrayRef.getId() != null) {
  sb.append(", byteArrayValueId=").append(byteArrayRef.getId());
 }
 sb.append("]");
 return sb.toString();
}

代码示例来源:origin: Activiti/Activiti

@Override
public String toString() {
 StringBuilder sb = new StringBuilder();
 sb.append("VariableInstanceEntity[");
 sb.append("id=").append(id);
 sb.append(", name=").append(name);
 sb.append(", type=").append(type != null ? type.getTypeName() : "null");
 if (longValue != null) {
  sb.append(", longValue=").append(longValue);
 }
 if (doubleValue != null) {
  sb.append(", doubleValue=").append(doubleValue);
 }
 if (textValue != null) {
  sb.append(", textValue=").append(StringUtils.abbreviate(textValue, 40));
 }
 if (textValue2 != null) {
  sb.append(", textValue2=").append(StringUtils.abbreviate(textValue2, 40));
 }
 if (byteArrayRef != null && byteArrayRef.getId() != null) {
  sb.append(", byteArrayValueId=").append(byteArrayRef.getId());
 }
 sb.append("]");
 return sb.toString();
}

代码示例来源:origin: Activiti/Activiti

@Override
public String toString() {
 StringBuilder sb = new StringBuilder();
 sb.append("HistoricVariableInstanceEntity[");
 sb.append("id=").append(id);
 sb.append(", name=").append(name);
 sb.append(", revision=").append(revision);
 sb.append(", type=").append(variableType != null ? variableType.getTypeName() : "null");
 if (longValue != null) {
  sb.append(", longValue=").append(longValue);
 }
 if (doubleValue != null) {
  sb.append(", doubleValue=").append(doubleValue);
 }
 if (textValue != null) {
  sb.append(", textValue=").append(StringUtils.abbreviate(textValue, 40));
 }
 if (textValue2 != null) {
  sb.append(", textValue2=").append(StringUtils.abbreviate(textValue2, 40));
 }
 if (byteArrayRef != null && byteArrayRef.getId() != null) {
  sb.append(", byteArrayValueId=").append(byteArrayRef.getId());
 }
 sb.append("]");
 return sb.toString();
}

代码示例来源:origin: org.apache.commons/commons-lang3

return abbrevMarker + abbreviate(str.substring(offset), abbrevMarker, maxWidth - abbrevMarkerLength);

代码示例来源:origin: Graylog2/graylog2-server

@Override
public String evaluate(FunctionArgs args, EvaluationContext context) {
  final String value = valueParam.required(args, context);
  final Long required = widthParam.required(args, context);
  if (required == null) {
    return null;
  }
  final Long maxWidth = Math.max(required, 4L);
  return StringUtils.abbreviate(value, saturatedCast(maxWidth));
}

代码示例来源:origin: pmd/pmd

/**
 * Shortens and escapes (for markdown) some special characters. Otherwise the shortened text
 * could contain some unfinished sequences.
 * @param rule
 * @return
 */
private static String getShortRuleDescription(Rule rule) {
  return StringEscapeUtils.escapeHtml4(
    StringUtils.abbreviate(
      StringUtils.stripToEmpty(
        rule.getDescription()
          .replaceAll("\n|\r", "")
          .replaceAll("\\|", "\\\\|")
          .replaceAll("`", "'")
          .replaceAll("\\*", "")),
      100));
}

代码示例来源:origin: ata4/disunity

String formatCell(Object value, int column) {
  int width = columnWidths.get(column);
  TextTableAlignment align = columnAlignments.get(column);
  Function<Object, String> formatter = columnFormatters.get(column);
  String content = formatter.apply(value);
  if (content.length() > width) {
    // truncate
    content = StringUtils.abbreviate(content, width);
  } else if (content.length() < width) {
    // add padding
    switch (align) {
      case LEFT:
        content = StringUtils.rightPad(content, width);
        break;
      case RIGHT:
        content = StringUtils.leftPad(content, width);
        break;
      case CENTER:
        content = StringUtils.center(content, width);
        break;
    }
  }
  return content;
}

代码示例来源:origin: apache/hbase

private static void logRetrievedMsg(final ZKWatcher zkw,
  final String znode, final byte [] data, final boolean watcherSet) {
 if (!LOG.isTraceEnabled()) {
  return;
 }
 LOG.trace(zkw.prefix("Retrieved " + ((data == null)? 0: data.length) +
  " byte(s) of data from znode " + znode +
  (watcherSet? " and set watcher; ": "; data=") +
  (data == null? "null": data.length == 0? "empty": (
    znode.startsWith(zkw.getZNodePaths().metaZNodePrefix)?
     getServerNameOrEmptyString(data):
    znode.startsWith(zkw.getZNodePaths().backupMasterAddressesZNode)?
     getServerNameOrEmptyString(data):
    StringUtils.abbreviate(Bytes.toStringBinary(data), 32)))));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testAbbreviate_StringInt() {
  assertNull(StringUtils.abbreviate(null, 10));
  assertEquals("", StringUtils.abbreviate("", 10));
  assertEquals("short", StringUtils.abbreviate("short", 10));
  assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
  final String raspberry = "raspberry peach";
  assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
  assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
  assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
  assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6));
  assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7));
  assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8));
  assertEquals("a...", StringUtils.abbreviate("abcdefg", 4));
  assertEquals("", StringUtils.abbreviate("", 4));
  try {
    StringUtils.abbreviate("abc", 3);
    fail("StringUtils.abbreviate expecting IllegalArgumentException");
  } catch (final IllegalArgumentException expected) {
    // empty
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

private void assertAbbreviateWithOffset(final String expected, final int offset, final int maxWidth) {
  final String abcdefghijklmno = "abcdefghijklmno";
  final String message = "abbreviate(String,int,int) failed";
  final String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
  if (offset >= 0 && offset < abcdefghijklmno.length()) {
    assertTrue(message + " -- should contain offset character",
        actual.indexOf((char) ('a' + offset)) != -1);
  }
  assertTrue(message + " -- should not be greater than maxWidth",
      actual.length() <= maxWidth);
  assertEquals(message, expected, actual);
}

代码示例来源:origin: org.apache.commons/commons-lang3

private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) {
  final String abcdefghijklmno = "abcdefghijklmno";
  final String message = "abbreviate(String,String,int,int) failed";
  final String actual = StringUtils.abbreviate(abcdefghijklmno, abbrevMarker, offset, maxWidth);
  if (offset >= 0 && offset < abcdefghijklmno.length()) {
    assertTrue(message + " -- should contain offset character",
        actual.indexOf((char) ('a' + offset)) != -1);
  }
  assertTrue(message + " -- should not be greater than maxWidth",
      actual.length() <= maxWidth);
  assertEquals(message, expected, actual);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testAbbreviate_StringStringInt() {
  assertNull(StringUtils.abbreviate(null, null, 10));
  assertNull(StringUtils.abbreviate(null, "...", 10));
  assertEquals("paranaguacu", StringUtils.abbreviate("paranaguacu", null, 10));
  assertEquals("", StringUtils.abbreviate("", "...", 2));
  assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
  assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10));
  final String raspberry = "raspberry peach";
  assertEquals("raspberry pe..", StringUtils.abbreviate(raspberry, "..", 14));
  assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", "---*---", 15));
  assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", ".", 16));
  assertEquals("abc()(", StringUtils.abbreviate("abcdefg", "()(", 6));
  assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", ";", 7));
  assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8));
  assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4));
  assertEquals("", StringUtils.abbreviate("", 4));
  try {
    @SuppressWarnings("unused")
    final
    String res = StringUtils.abbreviate("abcdefghij", "...", 3);
    fail("StringUtils.abbreviate expecting IllegalArgumentException");
  } catch (final IllegalArgumentException ex) {
    // empty
  }
}

代码示例来源:origin: kiegroup/optaplanner

private void writeRoomTalks(List<Timeslot> dayTimeslotList, Room room, List<Talk> roomTalkList) {
  Timeslot mergePreviousTimeslot = null;
  int mergeStart = -1;
  for (Timeslot timeslot : dayTimeslotList) {
    List<Talk> talkList = roomTalkList.stream()
        .filter(talk -> talk.getTimeslot() == timeslot).collect(toList());
    if (talkList.isEmpty() && mergePreviousTimeslot != null
        && timeslot.getStartDateTime().compareTo(mergePreviousTimeslot.getEndDateTime()) < 0) {
      nextCellVertically();
    } else {
      if (mergePreviousTimeslot != null && mergeStart < currentRowNumber) {
        currentSheet.addMergedRegion(new CellRangeAddress(mergeStart, currentRowNumber, currentColumnNumber, currentColumnNumber));
      }
      boolean unavailable = room.getUnavailableTimeslotSet().contains(timeslot)
          || Collections.disjoint(room.getTalkTypeSet(), timeslot.getTalkTypeSet());
      nextTalkListCell(unavailable, talkList, talk -> StringUtils.abbreviate(talk.getTitle(), 50) + "\n"
          + StringUtils.abbreviate(talk.getSpeakerList().stream().map(Speaker::getName).collect(joining(", ")), 30), true);
      mergePreviousTimeslot = talkList.isEmpty() ? null : timeslot;
      mergeStart = currentRowNumber;
    }
  }
  if (mergePreviousTimeslot != null && mergeStart < currentRowNumber) {
    currentSheet.addMergedRegion(new CellRangeAddress(mergeStart, currentRowNumber, currentColumnNumber, currentColumnNumber));
  }
}

代码示例来源:origin: kiegroup/optaplanner

int y = translator.translateLatitudeToY(airport.getLatitude());
g.fillRect(x - 1, y - 1, 3, 3);
g.drawString(StringUtils.abbreviate(airport.getCode(), 20), x + 3, y - 3);

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testAbbreviate_StringStringIntInt() {
  assertNull(StringUtils.abbreviate(null, null, 10, 12));
  assertNull(StringUtils.abbreviate(null, "...", 10, 12));
  assertEquals("", StringUtils.abbreviate("", null, 0, 10));
  assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
    StringUtils.abbreviate("abcdefghij", "::", 0, 2);
    fail("StringUtils.abbreviate expecting IllegalArgumentException");
  } catch (final IllegalArgumentException expected) {
    StringUtils.abbreviate("abcdefghij", "!!!", 5, 6);
    fail("StringUtils.abbreviate expecting IllegalArgumentException");
  } catch (final IllegalArgumentException expected) {
  assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15));
  assertNull(StringUtils.abbreviate(null, ";", 7, 14));
  assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh;;", ";;", -1, 10);
  assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi.", ".", 0, 10);

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testAbbreviate_StringIntInt() {
  assertNull(StringUtils.abbreviate(null, 10, 12));
  assertEquals("", StringUtils.abbreviate("", 0, 10));
  assertEquals("", StringUtils.abbreviate("", 2, 10));
    StringUtils.abbreviate("abcdefghij", 0, 3);
    fail("StringUtils.abbreviate expecting IllegalArgumentException");
  } catch (final IllegalArgumentException expected) {
    StringUtils.abbreviate("abcdefghij", 5, 6);
    fail("StringUtils.abbreviate expecting IllegalArgumentException");
  } catch (final IllegalArgumentException expected) {
  assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
  assertNull(StringUtils.abbreviate(null, 7, 14));
  assertAbbreviateWithOffset("abcdefg...", -1, 10);
  assertAbbreviateWithOffset("abcdefg...", 0, 10);

相关文章

微信公众号

最新文章

更多

StringUtils类方法