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

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

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

String.trim介绍

[英]Returns a copy of the string, with leading and trailing whitespace omitted.

If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).

This method may be used to trim whitespace (as defined above) from the beginning and end of a string.
[中]返回字符串的副本,并省略前导和尾随空格。
如果此String对象表示空字符序列,或者此String对象表示的字符序列的第一个和最后一个字符的代码都大于'\u0020'(空格字符),则返回对该String对象的引用。
否则,如果字符串中没有代码大于'\u0020'的字符,则将创建并返回表示空字符串的新String对象。
否则,k是代码大于'\u0020'的字符串中第一个字符的索引,m是代码大于'\u0020'的字符串中最后一个字符的索引。将创建一个新的String对象,表示此字符串的子字符串,该字符串以索引k处的字符开始,以索引m处的字符结束,即this.substring(k, m+1)的结果。
此方法可用于从字符串的开头和结尾修剪空白(如上所述)。

代码示例

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

/**
 * Set the namespace URI of the service.
 * Corresponds to the WSDL "targetNamespace".
 */
public void setNamespaceUri(@Nullable String namespaceUri) {
  this.namespaceUri = (namespaceUri != null ? namespaceUri.trim() : null);
}

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

public static boolean empty( final String s ) {
 // Null-safe, short-circuit evaluation.
 return s == null || s.trim().isEmpty();
}

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

/**
 * Add a field with the specified value without any validation. Only appropriate for headers
 * from the remote peer or cache.
 */
Builder addLenient(String name, String value) {
 namesAndValues.add(name);
 namesAndValues.add(value.trim());
 return this;
}

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

public Builder value(String value) {
 if (value == null) throw new NullPointerException("value == null");
 if (!value.trim().equals(value)) throw new IllegalArgumentException("value is not trimmed");
 this.value = value;
 return this;
}

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

public Builder name(String name) {
 if (name == null) throw new NullPointerException("name == null");
 if (!name.trim().equals(name)) throw new IllegalArgumentException("name is not trimmed");
 this.name = name;
 return this;
}

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

private static String getSubmittedFileName(Part part) {
  for (String cd : part.getHeader("content-disposition").split(";")) {
    if (cd.trim().startsWith("filename")) {
      String fileName = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
      return fileName.substring(fileName.lastIndexOf('/') + 1).substring(fileName.lastIndexOf('\\') + 1); // MSIE fix.
    }
  }
  return null;
}

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

String toChainString() {
  StringBuilder buf = new StringBuilder();
  PathElement pe = this.head;
  while (pe != null) {
    buf.append(pe.toString()).append(" ");
    pe = pe.next;
  }
  return buf.toString().trim();
}

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

private static boolean initCacheSectionFlag(String line, @Nullable LineInfo previousLine) {
  if (MANIFEST_SECTION_HEADERS.contains(line.trim())) {
    return line.trim().equals(CACHE_HEADER);
  }
  else if (previousLine != null) {
    return previousLine.isCacheSection();
  }
  throw new IllegalStateException(
      "Manifest does not start with " + MANIFEST_HEADER + ": " + line);
}

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

Props(Element root) {
  String defaultCache = root.getAttribute("cache");
  this.key = root.getAttribute("key");
  this.keyGenerator = root.getAttribute("key-generator");
  this.cacheManager = root.getAttribute("cache-manager");
  this.condition = root.getAttribute("condition");
  this.method = root.getAttribute(METHOD_ATTRIBUTE);
  if (StringUtils.hasText(defaultCache)) {
    this.caches = StringUtils.commaDelimitedListToStringArray(defaultCache.trim());
  }
}

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

/** Add an header line containing a field name, a literal colon, and a value. */
public Builder add(String line) {
 int index = line.indexOf(":");
 if (index == -1) {
  throw new IllegalArgumentException("Unexpected header: " + line);
 }
 return add(line.substring(0, index).trim(), line.substring(index + 1));
}

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

static BeanDefinition parseKeyGenerator(Element element, BeanDefinition def) {
  String name = element.getAttribute("key-generator");
  if (StringUtils.hasText(name)) {
    def.getPropertyValues().add("keyGenerator", new RuntimeBeanReference(name.trim()));
  }
  return def;
}

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

@Override
  @Nullable
  public String resolveStringValue(String strVal) throws BeansException {
    String resolved = this.helper.replacePlaceholders(strVal, this.resolver);
    if (trimValues) {
      resolved = resolved.trim();
    }
    return (resolved.equals(nullValue) ? null : resolved);
  }
}

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

private static void parseErrorHandler(Element element, BeanDefinition def) {
  String name = element.getAttribute("error-handler");
  if (StringUtils.hasText(name)) {
    def.getPropertyValues().add("errorHandler", new RuntimeBeanReference(name.trim()));
  }
}

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

public static Method findMethod(String desc, ClassLoader loader) {
  try {
    int lparen = desc.indexOf('(');
    int dot = desc.lastIndexOf('.', lparen);
    String className = desc.substring(0, dot).trim();
    String methodName = desc.substring(dot + 1, lparen).trim();
    return getClass(className, loader).getDeclaredMethod(methodName, parseTypes(desc, loader));
  }
  catch (ClassNotFoundException | NoSuchMethodException ex) {
    throw new CodeGenerationException(ex);
  }
}

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

@Override
  public String apply(String t1) {
    return t1.trim().toLowerCase();
  }
};

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

@Override
  public String apply(String t1) {
    return t1.trim().toLowerCase();
  }
};

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

public static Constructor findConstructor(String desc, ClassLoader loader) {
  try {
    int lparen = desc.indexOf('(');
    String className = desc.substring(0, lparen).trim();
    return getClass(className, loader).getConstructor(parseTypes(desc, loader));
  }
  catch (ClassNotFoundException | NoSuchMethodException ex) {
    throw new CodeGenerationException(ex);
  }
}

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

private List<String> splitCookie(String value) {
  List<String> list = new ArrayList<>();
  for (String s : value.split(";")){
    list.add(s.trim());
  }
  return list;
}

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

/**
 * Parse the {@code class} XML elements.
 */
protected void parseManagedClasses(Element persistenceUnit, SpringPersistenceUnitInfo unitInfo) {
  List<Element> classes = DomUtils.getChildElementsByTagName(persistenceUnit, MANAGED_CLASS_NAME);
  for (Element element : classes) {
    String value = DomUtils.getTextValue(element).trim();
    if (StringUtils.hasText(value)) {
      unitInfo.addManagedClassName(value);
    }
  }
}

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

@Test
public void noTrim() throws Exception {
  StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",",false,false);
  editor.setAsText("  0,1  , 2 ");
  Object value = editor.getValue();
  String[] array = (String[]) value;
  for (int i = 0; i < array.length; ++i) {
    assertEquals(3, array[i].length());
    assertEquals("" + i, array[i].trim());
  }
  assertEquals("  0,1  , 2 ", editor.getAsText());
}

相关文章