cn.hutool.core.util.StrUtil.trim()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(1247)

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

StrUtil.trim介绍

[英]除去字符串头尾部的空白,如果字符串是null,依然返回null

注意,和String.trim不同,此方法使用NumberUtil.isBlankChar 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。

trim(null)          = null 
trim("")            = "" 
trim("     ")       = "" 
trim("abc")         = "abc" 
trim("    abc    ") = "abc"

[中]除去字符串头尾部的空白,如果字符串是null,依然返回null
注意,和String.trim不同,此方法使用NumberUtil.isBlankChar 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。

trim(null)          = null 
trim("")            = "" 
trim("     ")       = "" 
trim("abc")         = "abc" 
trim("    abc    ") = "abc"

代码示例

代码示例来源:origin: looly/hutool

@Override
public Object edit(Cell cell, Object value) {
  if(value instanceof String) {
    return StrUtil.trim((String)value);
  }
  return value;
}

代码示例来源:origin: looly/hutool

@Override
public Object edit(Cell cell, Object value) {
  if(value instanceof String) {
    return StrUtil.trim((String)value);
  }
  return value;
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头尾部的空白,如果字符串是{@code null},返回<code>""</code>。
 *
 * <pre>
 * StrUtil.trimToEmpty(null)          = ""
 * StrUtil.trimToEmpty("")            = ""
 * StrUtil.trimToEmpty("     ")       = ""
 * StrUtil.trimToEmpty("abc")         = "abc"
 * StrUtil.trimToEmpty("    abc    ") = "abc"
 * </pre>
 *
 * @param str 字符串
 * @return 去除两边空白符后的字符串, 如果为null返回""
 * @since 3.1.1
 */
public static String trimToEmpty(CharSequence str) {
  return str == null ? EMPTY : trim(str);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头尾部的空白,如果字符串是{@code null},返回<code>""</code>。
 *
 * <pre>
 * StrUtil.trimToEmpty(null)          = ""
 * StrUtil.trimToEmpty("")            = ""
 * StrUtil.trimToEmpty("     ")       = ""
 * StrUtil.trimToEmpty("abc")         = "abc"
 * StrUtil.trimToEmpty("    abc    ") = "abc"
 * </pre>
 *
 * @param str 字符串
 * @return 去除两边空白符后的字符串, 如果为null返回""
 * @since 3.1.1
 */
public static String trimToEmpty(CharSequence str) {
  return str == null ? EMPTY : trim(str);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头尾部的空白,如果字符串是<code>null</code>,依然返回<code>null</code>。
 * 
 * <p>
 * 注意,和<code>String.trim</code>不同,此方法使用<code>NumberUtil.isBlankChar</code> 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
 * 
 * <pre>
 * trim(null)          = null
 * trim(&quot;&quot;)            = &quot;&quot;
 * trim(&quot;     &quot;)       = &quot;&quot;
 * trim(&quot;abc&quot;)         = &quot;abc&quot;
 * trim(&quot;    abc    &quot;) = &quot;abc&quot;
 * </pre>
 * 
 * @param str 要处理的字符串
 * 
 * @return 除去头尾空白的字符串,如果原字串为<code>null</code>,则返回<code>null</code>
 */
public static String trim(CharSequence str) {
  return (null == str) ? null : trim(str, 0);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串尾部的空白,如果字符串是<code>null</code>,则返回<code>null</code>。
 * 
 * <p>
 * 注意,和<code>String.trim</code>不同,此方法使用<code>CharUtil.isBlankChar</code> 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
 * 
 * <pre>
 * trimEnd(null)       = null
 * trimEnd(&quot;&quot;)         = &quot;&quot;
 * trimEnd(&quot;abc&quot;)      = &quot;abc&quot;
 * trimEnd(&quot;  abc&quot;)    = &quot;  abc&quot;
 * trimEnd(&quot;abc  &quot;)    = &quot;abc&quot;
 * trimEnd(&quot; abc &quot;)    = &quot; abc&quot;
 * </pre>
 * 
 * @param str 要处理的字符串
 * 
 * @return 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>""</code>,则返回 <code>null</code>
 */
public static String trimEnd(CharSequence str) {
  return trim(str, 1);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头尾部的空白,如果字符串是<code>null</code>,依然返回<code>null</code>。
 * 
 * <p>
 * 注意,和<code>String.trim</code>不同,此方法使用<code>NumberUtil.isBlankChar</code> 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
 * 
 * <pre>
 * trim(null)          = null
 * trim(&quot;&quot;)            = &quot;&quot;
 * trim(&quot;     &quot;)       = &quot;&quot;
 * trim(&quot;abc&quot;)         = &quot;abc&quot;
 * trim(&quot;    abc    &quot;) = &quot;abc&quot;
 * </pre>
 * 
 * @param str 要处理的字符串
 * 
 * @return 除去头尾空白的字符串,如果原字串为<code>null</code>,则返回<code>null</code>
 */
public static String trim(CharSequence str) {
  return (null == str) ? null : trim(str, 0);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头部的空白,如果字符串是<code>null</code>,则返回<code>null</code>。
 * 
 * <p>
 * 注意,和<code>String.trim</code>不同,此方法使用<code>CharUtil.isBlankChar</code> 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
 * 
 * <pre>
 * trimStart(null)         = null
 * trimStart(&quot;&quot;)           = &quot;&quot;
 * trimStart(&quot;abc&quot;)        = &quot;abc&quot;
 * trimStart(&quot;  abc&quot;)      = &quot;abc&quot;
 * trimStart(&quot;abc  &quot;)      = &quot;abc  &quot;
 * trimStart(&quot; abc &quot;)      = &quot;abc &quot;
 * </pre>
 * 
 * @param str 要处理的字符串
 * 
 * @return 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>""</code>,则返回 <code>null</code>
 */
public static String trimStart(CharSequence str) {
  return trim(str, -1);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串尾部的空白,如果字符串是<code>null</code>,则返回<code>null</code>。
 * 
 * <p>
 * 注意,和<code>String.trim</code>不同,此方法使用<code>CharUtil.isBlankChar</code> 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
 * 
 * <pre>
 * trimEnd(null)       = null
 * trimEnd(&quot;&quot;)         = &quot;&quot;
 * trimEnd(&quot;abc&quot;)      = &quot;abc&quot;
 * trimEnd(&quot;  abc&quot;)    = &quot;  abc&quot;
 * trimEnd(&quot;abc  &quot;)    = &quot;abc&quot;
 * trimEnd(&quot; abc &quot;)    = &quot; abc&quot;
 * </pre>
 * 
 * @param str 要处理的字符串
 * 
 * @return 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>""</code>,则返回 <code>null</code>
 */
public static String trimEnd(CharSequence str) {
  return trim(str, 1);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头部的空白,如果字符串是<code>null</code>,则返回<code>null</code>。
 * 
 * <p>
 * 注意,和<code>String.trim</code>不同,此方法使用<code>CharUtil.isBlankChar</code> 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
 * 
 * <pre>
 * trimStart(null)         = null
 * trimStart(&quot;&quot;)           = &quot;&quot;
 * trimStart(&quot;abc&quot;)        = &quot;abc&quot;
 * trimStart(&quot;  abc&quot;)      = &quot;abc&quot;
 * trimStart(&quot;abc  &quot;)      = &quot;abc  &quot;
 * trimStart(&quot; abc &quot;)      = &quot;abc &quot;
 * </pre>
 * 
 * @param str 要处理的字符串
 * 
 * @return 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>""</code>,则返回 <code>null</code>
 */
public static String trimStart(CharSequence str) {
  return trim(str, -1);
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头尾部的空白,如果字符串是{@code null},返回<code>""</code>。
 *
 * <pre>
 * StrUtil.trimToNull(null)          = null
 * StrUtil.trimToNull("")            = null
 * StrUtil.trimToNull("     ")       = null
 * StrUtil.trimToNull("abc")         = "abc"
 * StrUtil.trimToEmpty("    abc    ") = "abc"
 * </pre>
 *
 * @param str 字符串
 * @return 去除两边空白符后的字符串, 如果为空返回null
 * @since 3.2.1
 */
public static String trimToNull(CharSequence str) {
  final String trimStr = trim(str);
  return EMPTY.equals(trimStr) ? null : trimStr;
}

代码示例来源:origin: looly/hutool

/**
 * 除去字符串头尾部的空白,如果字符串是{@code null},返回<code>""</code>。
 *
 * <pre>
 * StrUtil.trimToNull(null)          = null
 * StrUtil.trimToNull("")            = null
 * StrUtil.trimToNull("     ")       = null
 * StrUtil.trimToNull("abc")         = "abc"
 * StrUtil.trimToEmpty("    abc    ") = "abc"
 * </pre>
 *
 * @param str 字符串
 * @return 去除两边空白符后的字符串, 如果为空返回null
 * @since 3.2.1
 */
public static String trimToNull(CharSequence str) {
  final String trimStr = trim(str);
  return EMPTY.equals(trimStr) ? null : trimStr;
}

代码示例来源:origin: looly/hutool

@Override
protected String convertToStr(Object value) {
  return StrUtil.trim(super.convertToStr(value));
}

代码示例来源:origin: looly/hutool

@Override
protected String convertToStr(Object value) {
  return StrUtil.trim(super.convertToStr(value));
}

代码示例来源:origin: looly/hutool

@Override
protected String convertToStr(Object value) {
  return StrUtil.trim(super.convertToStr(value));
}

代码示例来源:origin: looly/hutool

@Override
protected String convertToStr(Object value) {
  return StrUtil.trim(super.convertToStr(value));
}

代码示例来源:origin: looly/hutool

final String trimVal = StrUtil.trim(val);
if (false == val.equals(trimVal)) {

代码示例来源:origin: looly/hutool

final String trimVal = StrUtil.trim(val);
if (false == val.equals(trimVal)) {

代码示例来源:origin: looly/hutool

final String contentStr = StrUtil.trim(lastContent);

代码示例来源:origin: looly/hutool

final String contentStr = StrUtil.trim(lastContent);

相关文章

微信公众号

最新文章

更多

StrUtil类方法