com.google.common.base.Ascii.truncate()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(263)

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

Ascii.truncate介绍

[英]Truncates the given character sequence to the given maximum length. If the length of the sequence is greater than maxLength, the returned string will be exactly maxLength chars in length and will end with the given truncationIndicator. Otherwise, the sequence will be returned as a string with no changes to the content.

Examples:

Ascii.truncate("foobar", 7, "..."); // returns "foobar"

Note: This method may work with certain non-ASCII text but is not safe for use with arbitrary Unicode text. It is mostly intended for use with text that is known to be safe for use with it (such as all-ASCII text) and for simple debugging text. When using this method, consider the following:

  • it may split surrogate pairs
  • it may split characters and combining characters
  • it does not consider word boundaries
  • if truncating for display to users, there are other considerations that must be taken into account
  • the appropriate truncation indicator may be locale-dependent
  • it is safe to use non-ASCII characters in the truncation indicator
    [中]将给定的字符序列截断为给定的最大长度。如果序列的长度大于maxLength,则返回的字符串的长度正好是maxLength字符,并以给定的截断指示符结尾。否则,序列将作为字符串返回,内容不会发生任何更改。
    示例:
Ascii.truncate("foobar", 7, "..."); // returns "foobar"

注意:此方法可能适用于某些非ASCII文本,但不适合用于任意Unicode文本。它主要用于已知可安全使用的文本(如所有ASCII文本)和简单调试文本。使用此方法时,请考虑以下事项:
*它可以分割代理项对
*它可以拆分字符和组合字符
*不考虑单词边界
*如果截断以显示给用户,则必须考虑其他因素
*适当的截断指示符可能取决于语言环境
*在截断指示符中使用非ASCII字符是安全的

代码示例

代码示例来源:origin: google/guava

@Override
 public String toString() {
  return "CharSource.wrap(" + Ascii.truncate(seq, 30, "...") + ")";
 }
}

代码示例来源:origin: google/j2objc

@Override
 public String toString() {
  return "CharSource.wrap(" + Ascii.truncate(seq, 30, "...") + ")";
 }
}

代码示例来源:origin: google/guava

@Override
 public String toString() {
  return "ByteSource.wrap("
    + Ascii.truncate(BaseEncoding.base16().encode(bytes, offset, length), 30, "...")
    + ")";
 }
}

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

@Override
 public String toString() {
  return "CharSource.wrap(" + Ascii.truncate(seq, 30, "...") + ")";
 }
}

代码示例来源:origin: google/j2objc

@Override
 public String toString() {
  return "ByteSource.wrap("
    + Ascii.truncate(BaseEncoding.base16().encode(bytes, offset, length), 30, "...")
    + ")";
 }
}

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

@Override
 public String toString() {
  return "ByteSource.wrap("
    + Ascii.truncate(BaseEncoding.base16().encode(bytes, offset, length), 30, "...")
    + ")";
 }
}

代码示例来源:origin: google/guava

public void testTruncate() {
 assertEquals("foobar", Ascii.truncate("foobar", 10, "..."));
 assertEquals("fo...", Ascii.truncate("foobar", 5, "..."));
 assertEquals("foobar", Ascii.truncate("foobar", 6, "..."));
 assertEquals("...", Ascii.truncate("foobar", 3, "..."));
 assertEquals("foobar", Ascii.truncate("foobar", 10, "…"));
 assertEquals("foo…", Ascii.truncate("foobar", 4, "…"));
 assertEquals("fo--", Ascii.truncate("foobar", 4, "--"));
 assertEquals("foobar", Ascii.truncate("foobar", 6, "…"));
 assertEquals("foob…", Ascii.truncate("foobar", 5, "…"));
 assertEquals("foo", Ascii.truncate("foobar", 3, ""));
 assertEquals("", Ascii.truncate("", 5, ""));
 assertEquals("", Ascii.truncate("", 5, "..."));
 assertEquals("", Ascii.truncate("", 0, ""));
}

代码示例来源:origin: google/guava

public void testTruncateIllegalArguments() {
 String truncated = null;
 try {
  truncated = Ascii.truncate("foobar", 2, "...");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  truncated = Ascii.truncate("foobar", 8, "1234567890");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  truncated = Ascii.truncate("foobar", -1, "...");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  truncated = Ascii.truncate("foobar", -1, "");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: spotify/helios

public void task(final JobId jobId, final String host, final TaskStatus ts,
         final Deployment deployment) {
 final String goal = (deployment == null) ? "" : deployment.getGoal().toString();
 final int maxContainerId = full ? Integer.MAX_VALUE : 7;
 final String jobIdString = full ? jobId.toString() : jobId.toShortString();
 if (ts == null) {
  table.row(jobIdString, host, goal, "", "", "");
 } else {
  final List<String> portMappings = new ArrayList<>();
  for (final Map.Entry<String, PortMapping> entry : ts.getPorts().entrySet()) {
   final PortMapping portMapping = entry.getValue();
   portMappings.add(String.format("%s=%d:%d", entry.getKey(),
     portMapping.getInternalPort(),
     portMapping.getExternalPort()));
  }
  String state = ts.getState().toString();
  if (ts.getThrottled() != ThrottleState.NO) {
   state += " (" + ts.getThrottled() + ")";
  }
  final String ports = Joiner.on(" ").join(portMappings);
  final String cid = truncate(fromNullable(ts.getContainerId()).or(""), maxContainerId, "");
  table.row(jobIdString, host, goal, state, cid, ports);
 }
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

@Override
 public String toString() {
  return "CharSource.wrap(" + Ascii.truncate(seq, 30, "...") + ")";
 }
}

代码示例来源:origin: org.kill-bill.billing/killbill-platform-osgi-bundles-logger

@Override
 public String toString() {
  return "CharSource.wrap(" + Ascii.truncate(seq, 30, "...") + ")";
 }
}

代码示例来源:origin: com.google.guava/guava-jdk5

@Override
 public String toString() {
  return "CharSource.wrap(" + Ascii.truncate(seq, 30, "...") + ")";
 }
}

代码示例来源:origin: org.kill-bill.billing/killbill-platform-osgi-bundles-logger

@Override
 public String toString() {
  return "ByteSource.wrap("
    + Ascii.truncate(BaseEncoding.base16().encode(bytes, offset, length), 30, "...")
    + ")";
 }
}

代码示例来源:origin: com.google.guava/guava-jdk5

@Override
 public String toString() {
  return "ByteSource.wrap("
    + Ascii.truncate(BaseEncoding.base16().encode(bytes), 30, "...") + ")";
 }
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

@Override
 public String toString() {
  return "ByteSource.wrap("
    + Ascii.truncate(BaseEncoding.base16().encode(bytes, offset, length), 30, "...")
    + ")";
 }
}

代码示例来源:origin: com.enonic.xp/portal-api

@Override
public String toString()
{
  return MoreObjects.toStringHelper( this ).
    add( "name", name ).
    add( "body", body ).
    add( "params", params ).
    add( "request", request ).
    add( "document", Ascii.truncate( document, 20, "..." ) ).
    toString();
}

代码示例来源:origin: triplea-game/triplea

private void addChatMessage(final String originalMessage, final String from, final boolean thirdperson) {
 final String message = Ascii.truncate(originalMessage, 200, "...");
 final String time = "(" + TimeManager.getLocalizedTime() + ")";
 final String prefix = thirdperson ? (showTime ? "* " + time + " " + from : "* " + from)
   : (showTime ? time + " " + from + ": " : from + ": ");
 final String fullMessage = prefix + " " + message + "\n";
 final String currentAllText = allText.toString();
 if (currentAllText.length() > MAX_LENGTH) {
  allText = new StringBuilder(currentAllText.substring(MAX_LENGTH / 2));
 }
 allText.append(fullMessage);
}

代码示例来源:origin: com.google.guava/guava-tests

public void testTruncate() {
 assertEquals("foobar", Ascii.truncate("foobar", 10, "..."));
 assertEquals("fo...", Ascii.truncate("foobar", 5, "..."));
 assertEquals("foobar", Ascii.truncate("foobar", 6, "..."));
 assertEquals("...", Ascii.truncate("foobar", 3, "..."));
 assertEquals("foobar", Ascii.truncate("foobar", 10, "…"));
 assertEquals("foo…", Ascii.truncate("foobar", 4, "…"));
 assertEquals("fo--", Ascii.truncate("foobar", 4, "--"));
 assertEquals("foobar", Ascii.truncate("foobar", 6, "…"));
 assertEquals("foob…", Ascii.truncate("foobar", 5, "…"));
 assertEquals("foo", Ascii.truncate("foobar", 3, ""));
 assertEquals("", Ascii.truncate("", 5, ""));
 assertEquals("", Ascii.truncate("", 5, "..."));
 assertEquals("", Ascii.truncate("", 0, ""));
}

代码示例来源:origin: com.google.guava/guava-tests

public void testTruncateIllegalArguments() {
 String truncated = null;
 try {
  truncated = Ascii.truncate("foobar", 2, "...");
  fail();
 } catch (IllegalArgumentException expected) {}
 try {
  truncated = Ascii.truncate("foobar", 8, "1234567890");
  fail();
 } catch (IllegalArgumentException expected) {}
 try {
  truncated = Ascii.truncate("foobar", -1, "...");
  fail();
 } catch (IllegalArgumentException expected) {}
 try {
  truncated = Ascii.truncate("foobar", -1, "");
  fail();
 } catch (IllegalArgumentException expected) {}
}

代码示例来源:origin: at.molindo/helios-tools

public void task(final JobId jobId, final String host, final TaskStatus ts,
         final Deployment deployment) {
 final String goal = (deployment == null) ? "" : deployment.getGoal().toString();
 final int maxContainerId = full ? Integer.MAX_VALUE : 7;
 final String jobIdString = full ? jobId.toString() : jobId.toShortString();
 if (ts == null) {
  table.row(jobIdString, host, goal, "", "", "");
 } else {
  final List<String> portMappings = new ArrayList<>();
  for (Map.Entry<String, PortMapping> entry : ts.getPorts().entrySet()) {
   final PortMapping portMapping = entry.getValue();
   portMappings.add(String.format("%s=%d:%d", entry.getKey(),
                   portMapping.getInternalPort(),
                   portMapping.getExternalPort()));
  }
  String state = ts.getState().toString();
  if (ts.getThrottled() != ThrottleState.NO) {
   state += " (" + ts.getThrottled() + ")";
  }
  final String ports = Joiner.on(" ").join(portMappings);
  final String cid = truncate(fromNullable(ts.getContainerId()).or(""), maxContainerId, "");
  table.row(jobIdString, host, goal, state, cid, ports);
 }
}

相关文章