本文整理了Java中com.google.common.base.Ascii.truncate()
方法的一些代码示例,展示了Ascii.truncate()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Ascii.truncate()
方法的具体详情如下:
包路径:com.google.common.base.Ascii
类名称: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:
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);
}
}
内容来源于网络,如有侵权,请联系作者删除!