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

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

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

String.endsWith介绍

[英]Tests if this string ends with the specified suffix.
[中]测试此字符串是否以指定的后缀结尾。

代码示例

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

private String contentType(String path) {
 if (path.endsWith(".png")) return "image/png";
 if (path.endsWith(".jpg")) return "image/jpeg";
 if (path.endsWith(".jpeg")) return "image/jpeg";
 if (path.endsWith(".gif")) return "image/gif";
 if (path.endsWith(".html")) return "text/html; charset=utf-8";
 if (path.endsWith(".txt")) return "text/plain; charset=utf-8";
 return "application/octet-stream";
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Configure the prefix to use for destinations targeting a specific user.
 * <p>The default value is "/user/".
 * @see org.springframework.messaging.simp.user.UserDestinationMessageHandler
 */
public void setUserDestinationPrefix(String prefix) {
  Assert.hasText(prefix, "User destination prefix must not be empty");
  this.destinationPrefix = (prefix.endsWith("/") ? prefix : prefix + "/");
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public String addVersion(String path, String version) {
  if (path.startsWith(".")) {
    return path;
  }
  else if (this.prefix.endsWith("/") || path.startsWith("/")) {
    return this.prefix + path;
  }
  else {
    return this.prefix + '/' + path;
  }
}

代码示例来源:origin: spring-projects/spring-framework

private String padEtagIfNecessary(String etag) {
  if (!StringUtils.hasLength(etag)) {
    return etag;
  }
  if ((etag.startsWith("\"") || etag.startsWith("W/\"")) && etag.endsWith("\"")) {
    return etag;
  }
  return "\"" + etag + "\"";
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public String toString() {
  String result = this.builder.toString();
  if (result.endsWith("\n")) {
    result = result.substring(0, result.length() - 1);
  }
  return result;
}

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

@Override
 Optional<String> chop(String str) {
  if (str.endsWith(suffix)) {
   return Optional.of(str.substring(0, str.length() - suffix.length()));
  } else {
   return Optional.absent();
  }
 }
};

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

private static boolean domainMatch(String urlHost, String domain) {
 if (urlHost.equals(domain)) {
  return true; // As in 'example.com' matching 'example.com'.
 }
 if (urlHost.endsWith(domain)
   && urlHost.charAt(urlHost.length() - domain.length() - 1) == '.'
   && !verifyAsIpAddress(urlHost)) {
  return true; // As in 'example.com' matching 'www.example.com'.
 }
 return false;
}

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

private static boolean pathMatch(HttpUrl url, String path) {
 String urlPath = url.encodedPath();
 if (urlPath.equals(path)) {
  return true; // As in '/foo' matching '/foo'.
 }
 if (urlPath.startsWith(path)) {
  if (path.endsWith("/")) return true; // As in '/' matching '/foo'.
  if (urlPath.charAt(path.length()) == '/') return true; // As in '/foo' matching '/foo/bar'.
 }
 return false;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public EntityResponse.Builder<T> eTag(String etag) {
  if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
    etag = "\"" + etag;
  }
  if (!etag.endsWith("\"")) {
    etag = etag + "\"";
  }
  this.headers.setETag(etag);
  return this;
}

代码示例来源:origin: spring-projects/spring-framework

public PatternInfo(@Nullable String pattern) {
  this.pattern = pattern;
  if (this.pattern != null) {
    initCounters();
    this.catchAllPattern = this.pattern.equals("/**");
    this.prefixPattern = !this.catchAllPattern && this.pattern.endsWith("/**");
  }
  if (this.uriVars == 0) {
    this.length = (this.pattern != null ? this.pattern.length() : 0);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Determine whether the given class should be excluded from shadowing.
 * @param className the name of the class
 * @return whether the specified class should be shadowed
 */
private boolean shouldShadow(String className) {
  return (!className.equals(getClass().getName()) && !className.endsWith("ShadowingClassLoader") &&
      isEligibleForShadowing(className));
}

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

static ResourceInfo of(String resourceName, ClassLoader loader) {
 if (resourceName.endsWith(CLASS_FILE_NAME_EXTENSION)) {
  return new ClassInfo(resourceName, loader);
 } else {
  return new ResourceInfo(resourceName, loader);
 }
}

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

File directoryOf(String baseClassName) throws Exception {
  File f = MaybeNo2Dot0Since.findSource("Flowable");
  if (f == null) {
    return null;
  }
  String parent = f.getParentFile().getAbsolutePath().replace('\\', '/');
  if (!parent.endsWith("/")) {
    parent += "/";
  }
  parent += "internal/operators/" + baseClassName.toLowerCase() + "/";
  return new File(parent);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Context createSubcontext(String name) {
  String subcontextName = this.root + name;
  if (!subcontextName.endsWith("/")) {
    subcontextName += "/";
  }
  Context subcontext = new SimpleNamingContext(subcontextName, this.boundObjects, this.environment);
  bind(name, subcontext);
  return subcontext;
}

代码示例来源:origin: spring-projects/spring-framework

private void assertToNumberOverflow(Number number, Class<? extends Number> targetClass) {
  String msg = "Expected exception due to overflow: from=" + number + ", toClass=" + targetClass;
  try {
    NumberUtils.convertNumberToTargetClass(number, targetClass);
    fail(msg);
  }
  catch (IllegalArgumentException expected) {
    assertTrue(msg + ", with \"overflow\" in message but got message=" + expected.getMessage(),
        expected.getMessage().endsWith("overflow"));
  }
}

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

@GwtIncompatible // reflection
public void testConstants_charset() throws Exception {
 for (Field field : getConstantFields()) {
  Optional<Charset> charset = ((MediaType) field.get(null)).charset();
  if (field.getName().endsWith("_UTF_8")) {
   assertThat(charset).hasValue(UTF_8);
  } else {
   assertThat(charset).isAbsent();
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testBindLocalDateTime() {
  MutablePropertyValues propertyValues = new MutablePropertyValues();
  propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
  binder.bind(propertyValues);
  assertEquals(0, binder.getBindingResult().getErrorCount());
  String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
  assertTrue(value.startsWith("10/31/09"));
  assertTrue(value.endsWith("12:00 PM"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testBindLocalDateTimeAnnotated() {
  MutablePropertyValues propertyValues = new MutablePropertyValues();
  propertyValues.add("localDateTimeAnnotated", LocalDateTime.of(2009, 10, 31, 12, 0));
  binder.bind(propertyValues);
  assertEquals(0, binder.getBindingResult().getErrorCount());
  String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
  assertTrue(value.startsWith("Oct 31, 2009"));
  assertTrue(value.endsWith("12:00:00 PM"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testBindLocalDateTimeFromJavaUtilCalendar() {
  MutablePropertyValues propertyValues = new MutablePropertyValues();
  propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0));
  binder.bind(propertyValues);
  assertEquals(0, binder.getBindingResult().getErrorCount());
  String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
  assertTrue(value.startsWith("10/31/09"));
  assertTrue(value.endsWith("12:00 PM"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void connectWebSocketDisabled() throws URISyntaxException {
  setupInfoRequest(false);
  this.sockJsClient.doHandshake(handler, URL);
  assertFalse(this.webSocketTransport.invoked());
  assertTrue(this.xhrTransport.invoked());
  assertTrue(this.xhrTransport.getRequest().getTransportUrl().toString().endsWith("xhr_streaming"));
}

相关文章