java.lang.String.format()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.4k)|赞(0)|评价(0)|浏览(335)

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

String.format介绍

[英]Returns a formatted string using the specified format string and arguments.

The locale always used is the one returned by java.util.Locale#getDefault().
[中]使用指定的格式字符串和参数返回格式化字符串。
始终使用的语言环境是java返回的语言环境。util。语言环境#getDefault()。

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

@Override
 public String toString() {
  return String.format("Oliphaunt id=%d", id);
 }
}

代码示例来源:origin: square/okhttp

@Override public String toString() {
  return String.format("(ok=%s, access_token=%s, scope=%s, user_id=%s, team_name=%s, team_id=%s)",
    ok, access_token, scope, user_id, team_name, team_id);
 }
}

代码示例来源:origin: stackoverflow.com

String url = "http://example.com";
String charset = "UTF-8";  // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
String param1 = "value1";
String param2 = "value2";
// ...

String query = String.format("param1=%s&param2=%s", 
   URLEncoder.encode(param1, charset), 
   URLEncoder.encode(param2, charset));

代码示例来源:origin: iluwatar/java-design-patterns

@Override
 public synchronized String toString() {
  return String.format("Pool available=%d inUse=%d", available.size(), inUse.size());
 }
}

代码示例来源:origin: stackoverflow.com

public static String humanReadableByteCount(long bytes, boolean si) {
  int unit = si ? 1000 : 1024;
  if (bytes < unit) return bytes + " B";
  int exp = (int) (Math.log(bytes) / Math.log(unit));
  String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
  return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

代码示例来源:origin: stackoverflow.com

String.format("%d min, %d sec", 
  TimeUnit.MILLISECONDS.toMinutes(millis),
  TimeUnit.MILLISECONDS.toSeconds(millis) - 
  TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);

代码示例来源:origin: stackoverflow.com

String.format("%02d min, %02d sec", 
  TimeUnit.MILLISECONDS.toMinutes(millis),
  TimeUnit.MILLISECONDS.toSeconds(millis) - 
  TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);

代码示例来源:origin: square/retrofit

@Override public String toString() {
  return String.format("%s.%s() %s",
    method.getDeclaringClass().getName(), method.getName(), arguments);
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public String toString() {
    return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public String toString() {
    return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public String toString() {
    return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public String toString() {
    return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public String toString() {
    return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
  }
}

代码示例来源:origin: square/okhttp

@Override public Response intercept(Chain chain) throws IOException {
  long t1 = System.nanoTime();
  Request request = chain.request();
  logger.info(String.format("Sending request %s on %s%n%s",
    request.url(), chain.connection(), request.headers()));
  Response response = chain.proceed(request);
  long t2 = System.nanoTime();
  logger.info(String.format("Received response for %s in %.1fms%n%s",
    request.url(), (t2 - t1) / 1e6d, response.headers()));
  return response;
 }
}

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

protected final NetworkConnections<N, E> checkedConnections(N node) {
 NetworkConnections<N, E> connections = nodeConnections.get(node);
 if (connections == null) {
  checkNotNull(node);
  throw new IllegalArgumentException(String.format(NODE_NOT_IN_GRAPH, node));
 }
 return connections;
}

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

protected final N checkedReferenceNode(E edge) {
 N referenceNode = edgeToReferenceNode.get(edge);
 if (referenceNode == null) {
  checkNotNull(edge);
  throw new IllegalArgumentException(String.format(EDGE_NOT_IN_GRAPH, edge));
 }
 return referenceNode;
}

代码示例来源:origin: square/retrofit

private static void printContributors(GitHub gitHub, String owner, String repo)
   throws IOException {
  System.out.println(String.format("== Contributors for %s/%s ==", owner, repo));
  Call<List<Contributor>> contributors = gitHub.contributors(owner, repo);
  for (Contributor contributor : contributors.execute().body()) {
   System.out.println(contributor.login + " (" + contributor.contributions + ")");
  }
  System.out.println();
 }
}

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

private static String hmacToString(String methodName, Key key) {
 return String.format(
   "Hashing.%s(Key[algorithm=%s, format=%s])",
   methodName, key.getAlgorithm(), key.getFormat());
}

代码示例来源:origin: square/okhttp

@Override public void writeTo(BufferedSink sink) throws IOException {
 sink.writeUtf8("Numbers\n");
 sink.writeUtf8("-------\n");
 for (int i = 2; i <= 997; i++) {
  sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
 }
}

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

public void testKnownUtf8Hashing() {
 for (Cell<HashFunction, String, String> cell : KNOWN_HASHES.cellSet()) {
  HashFunction func = cell.getRowKey();
  String input = cell.getColumnKey();
  String expected = cell.getValue();
  assertEquals(
    String.format(Locale.ROOT, "Known hash for hash(%s, UTF_8) failed", input),
    expected,
    func.hashString(input, UTF_8).toString());
 }
}

相关文章