org.apache.commons.lang3.StringUtils.trim()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(246)

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

StringUtils.trim介绍

[英]Removes control characters (char <= 32) from both ends of this String, handling null by returning null.

The String is trimmed using String#trim(). Trim removes start and end characters <= 32. To strip whitespace use #strip(String).

To trim your choice of characters, use the #strip(String,String) methods.

StringUtils.trim(null)          = null 
StringUtils.trim("")            = "" 
StringUtils.trim("     ")       = "" 
StringUtils.trim("abc")         = "abc" 
StringUtils.trim("    abc    ") = "abc"

[中]从该字符串的两端删除控制字符(char<=32),通过返回null来处理null。
使用字符串#trim()修剪字符串。Trim删除起始字符和结束字符<=32。要去除空白,请使用#strip(String)。
要修剪您选择的字符,请使用#strip(String,String)方法。

StringUtils.trim(null)          = null 
StringUtils.trim("")            = "" 
StringUtils.trim("     ")       = "" 
StringUtils.trim("abc")         = "abc" 
StringUtils.trim("    abc    ") = "abc"

代码示例

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

public void setSrc(String src) {
  this.src = StringUtils.trim(src);
}

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

public void setDest(String dest) {
  this.dest = StringUtils.trim(dest);
}

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

public void setName(String name) {
  this.name = StringUtils.trim(name);
}

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

public void setSource(String source) {
  this.source = StringUtils.trim(source);
}

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

public void setEmail(String email) {
  this.email = StringUtils.trim(email);
}

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

public void setDestination(String destination) {
  if (StringUtils.isNotBlank(destination)) {
    this.destination = StringUtils.trim(destination);
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Removes control characters (char &lt;= 32) from both
 * ends of this String returning {@code null} if the String is
 * empty ("") after the trim or if it is {@code null}.
 *
 * <p>The String is trimmed using {@link String#trim()}.
 * Trim removes start and end characters &lt;= 32.
 * To strip whitespace use {@link #stripToNull(String)}.</p>
 *
 * <pre>
 * StringUtils.trimToNull(null)          = null
 * StringUtils.trimToNull("")            = null
 * StringUtils.trimToNull("     ")       = null
 * StringUtils.trimToNull("abc")         = "abc"
 * StringUtils.trimToNull("    abc    ") = "abc"
 * </pre>
 *
 * @param str  the String to be trimmed, may be null
 * @return the trimmed String,
 *  {@code null} if only chars &lt;= 32, empty or null String input
 * @since 2.0
 */
public static String trimToNull(final String str) {
  final String ts = trim(str);
  return isEmpty(ts) ? null : ts;
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * @return A map that contains default charset for a specific content-type. It will have precedence over {@link #defaultContentCharset()}.
 */
public boolean hasDefaultCharsetForContentType(String contentType) {
  return !StringUtils.isBlank(contentType) && contentTypeToDefaultCharset.containsKey(trim(contentType).toLowerCase());
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * @return A map that contains default charset for a specific content-type. It will have precedence over {@link #defaultContentCharset()}.
 */
public boolean hasDefaultCharsetForContentType(String contentType) {
  return !StringUtils.isBlank(contentType) && contentTypeToDefaultCharset.containsKey(trim(contentType).toLowerCase());
}

代码示例来源:origin: alibaba/nacos

@JSONField(serialize = false)
public Map<String, String> getCustomHeaders() {
  if (StringUtils.isBlank(headers)) {
    return Collections.emptyMap();
  }
  Map<String, String> headerMap = new HashMap<String, String>(16);
  for (String s : headers.split(Constants.NAMING_HTTP_HEADER_SPILIER)) {
    String[] splits = s.split(":");
    if (splits.length != 2) {
      continue;
    }
    headerMap.put(StringUtils.trim(splits[0]), StringUtils.trim(splits[1]));
  }
  return headerMap;
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * Specify a charset for this content-type
 *
 * @param charset The charset
 * @return The content-type with the given charset.
 */
public String withCharset(String charset) {
  if (StringUtils.isBlank(charset)) {
    throw new IllegalArgumentException("charset cannot be empty");
  }
  return format("%s; charset=%s", this.toString(), trim(charset));
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * Specify the default charset to use for the specific content-type if it's not specified in the content-type header explicitly
 *
 * @param charset     The charset to use as default (unless specified explicitly)
 * @param contentType The content-type
 * @return A new instance of {@link DecoderConfig}
 */
public DecoderConfig defaultCharsetForContentType(String charset, String contentType) {
  notNull(charset, "Charset");
  notNull(contentType, "ContentType");
  Map<String, String> map = new HashMap<String, String>(contentTypeToDefaultCharset);
  map.put(trim(contentType).toLowerCase(), trim(charset));
  return new DecoderConfig(charset, useNoWrapForInflateDecoding, true, contentDecoders, map);
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * Specify the default charset to use for the specific content-type if it's not specified in the content-type header explicitly
 *
 * @param charset     The charset to use as default (unless specified explicitly)
 * @param contentType The content-type
 * @return A new instance of {@link EncoderConfig}
 */
public EncoderConfig defaultCharsetForContentType(String charset, String contentType) {
  notNull(charset, "Charset");
  notNull(contentType, "ContentType");
  Map<String, String> map = new HashMap<String, String>(contentTypeToDefaultCharset);
  map.put(trim(contentType).toLowerCase(), trim(charset));
  return new EncoderConfig(charset, defaultQueryParameterCharset, shouldAppendDefaultContentCharsetToContentTypeIfUndefined, contentEncoders, map, true);
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * @return The default charset for a specific content-type. It will have precedence over {@link #defaultContentCharset()}.
 */
public String defaultCharsetForContentType(String contentType) {
  if (StringUtils.isEmpty(contentType)) {
    return defaultContentCharset();
  }
  String charset = contentTypeToDefaultCharset.get(trim(contentType).toLowerCase());
  if (charset == null) {
    return defaultContentCharset();
  }
  return charset;
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * @return The default charset for a specific content-type. It will have precedence over {@link #defaultContentCharset()}.
 */
public String defaultCharsetForContentType(String contentType) {
  if (StringUtils.isEmpty(contentType)) {
    return defaultContentCharset();
  }
  String charset = contentTypeToDefaultCharset.get(trim(contentType).toLowerCase());
  if (charset == null) {
    return defaultContentCharset();
  }
  return charset;
}

代码示例来源:origin: rest-assured/rest-assured

public CustomHttpMethod(String methodName, final URI uri) {
  AssertParameter.notNull(methodName, "Method");
  this.methodName = StringUtils.trim(methodName).toUpperCase();
  setURI(uri);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testTrim() {
  assertEquals(FOO, StringUtils.trim(FOO + "  "));
  assertEquals(FOO, StringUtils.trim(" " + FOO + "  "));
  assertEquals(FOO, StringUtils.trim(" " + FOO));
  assertEquals(FOO, StringUtils.trim(FOO + ""));
  assertEquals("", StringUtils.trim(" \t\r\n\b "));
  assertEquals("", StringUtils.trim(StringUtilsTest.TRIMMABLE));
  assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trim(StringUtilsTest.NON_TRIMMABLE));
  assertEquals("", StringUtils.trim(""));
  assertNull(StringUtils.trim(null));
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * Specify the default charset to use for the specific content-type if it's not specified in the content-type header explicitly
 *
 * @param charset     The charset to use as default (unless specified explicitly)
 * @param contentType The content-type
 * @return A new instance of {@link EncoderConfig}
 */
public EncoderConfig defaultCharsetForContentType(String charset, ContentType contentType) {
  notNull(charset, "Charset");
  notNull(contentType, ContentType.class);
  Map<String, String> map = new HashMap<String, String>(contentTypeToDefaultCharset);
  for (String ct : contentType.getContentTypeStrings()) {
    map.put(ct.toLowerCase(), trim(charset));
  }
  return new EncoderConfig(charset, defaultQueryParameterCharset, shouldAppendDefaultContentCharsetToContentTypeIfUndefined, contentEncoders, map, true);
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * Specify the default charset to use for the specific content-type if it's not specified in the content-type header explicitly
 *
 * @param charset     The charset to use as default (unless specified explicitly)
 * @param contentType The content-type
 * @return A new instance of {@link DecoderConfig}
 */
public DecoderConfig defaultCharsetForContentType(String charset, ContentType contentType) {
  notNull(charset, "Charset");
  notNull(contentType, ContentType.class);
  Map<String, String> map = new HashMap<String, String>(contentTypeToDefaultCharset);
  for (String ct : contentType.getContentTypeStrings()) {
    map.put(ct.toLowerCase(), trim(charset));
  }
  return new DecoderConfig(charset, useNoWrapForInflateDecoding, true, contentDecoders, map);
}

代码示例来源:origin: rest-assured/rest-assured

/**
 * Set a specific header
 *
 * @return The builder
 */
public ResponseBuilder setHeader(String name, String value) {
  notNull(name, "Header name");
  notNull(value, "Header value");
  List<Header> newHeaders = new ArrayList<Header>(restAssuredResponse.headers().asList());
  newHeaders.add(new Header(name, value));
  restAssuredResponse.setResponseHeaders(new Headers(newHeaders));
  if (trim(name).equalsIgnoreCase(CONTENT_TYPE)) {
    restAssuredResponse.setContentType(value);
  }
  return this;
}

相关文章

StringUtils类方法