当前位置: 移动技术网 > IT编程>开发语言>Java > Java List集合返回值去掉中括号(''[ ]'')的操作

Java List集合返回值去掉中括号(''[ ]'')的操作

2020年08月29日  | 移动技术网IT编程  | 我要评论
如下所示:调用stringutils工具类的strip()方法去掉中括号"[ ]":stringutils.strip(word.tostring(),"[]") //第一个参数放集合,第二个参数去掉

如下所示:

调用stringutils工具类的strip()方法去掉中括号"[ ]":

stringutils.strip(word.tostring(),"[]") //第一个参数放集合,第二个参数去掉中括号"[]"

stringutils工具类代码:

package com.ktamr.common.utils; 
import com.ktamr.common.core.text.strformatter; 
import java.util.collection;
import java.util.map;
 
/**
 * 字符串工具类
 * 
 * @author ktamr
 */
public class stringutils extends org.apache.commons.lang3.stringutils
{
 /** 空字符串 */
 private static final string nullstr = "";
 
 /** 下划线 */
 private static final char separator = '_';
 
 /**
  * 获取参数不为空值
  * 
  * @param value defaultvalue 要判断的value
  * @return value 返回值
  */
 public static <t> t nvl(t value, t defaultvalue)
 {
  return value != null ? value : defaultvalue;
 }
 
 /**
  * * 判断一个collection是否为空, 包含list,set,queue
  * 
  * @param coll 要判断的collection
  * @return true:为空 false:非空
  */
 public static boolean isempty(collection<?> coll)
 {
  return isnull(coll) || coll.isempty();
 }
 
 /**
  * * 判断一个collection是否非空,包含list,set,queue
  * 
  * @param coll 要判断的collection
  * @return true:非空 false:空
  */
 public static boolean isnotempty(collection<?> coll)
 {
  return !isempty(coll);
 }
 
 /**
  * * 判断一个对象数组是否为空
  * 
  * @param objects 要判断的对象数组
  ** @return true:为空 false:非空
  */
 public static boolean isempty(object[] objects)
 {
  return isnull(objects) || (objects.length == 0);
 }
 
 /**
  * * 判断一个对象数组是否非空
  * 
  * @param objects 要判断的对象数组
  * @return true:非空 false:空
  */
 public static boolean isnotempty(object[] objects)
 {
  return !isempty(objects);
 }
 
 /**
  * * 判断一个map是否为空
  * 
  * @param map 要判断的map
  * @return true:为空 false:非空
  */
 public static boolean isempty(map<?, ?> map)
 {
  return isnull(map) || map.isempty();
 }
 
 /**
  * * 判断一个map是否为空
  * 
  * @param map 要判断的map
  * @return true:非空 false:空
  */
 public static boolean isnotempty(map<?, ?> map)
 {
  return !isempty(map);
 }
 
 /**
  * * 判断一个字符串是否为空串
  * 
  * @param str string
  * @return true:为空 false:非空
  */
 public static boolean isempty(string str)
 {
  return isnull(str) || nullstr.equals(str.trim());
 }
 
 /**
  * * 判断一个字符串是否为非空串
  * 
  * @param str string
  * @return true:非空串 false:空串
  */
 public static boolean isnotempty(string str)
 {
  return !isempty(str);
 }
 
 /**
  * * 判断一个对象是否为空
  * 
  * @param object object
  * @return true:为空 false:非空
  */
 public static boolean isnull(object object)
 {
  return object == null;
 }
 
 /**
  * * 判断一个对象是否非空
  * 
  * @param object object
  * @return true:非空 false:空
  */
 public static boolean isnotnull(object object)
 {
  return !isnull(object);
 }
 
 /**
  * * 判断一个对象是否是数组类型(java基本型别的数组)
  * 
  * @param object 对象
  * @return true:是数组 false:不是数组
  */
 public static boolean isarray(object object)
 {
  return isnotnull(object) && object.getclass().isarray();
 }
 
 /**
  * 去空格
  */
 public static string trim(string str)
 {
  return (str == null ? "" : str.trim());
 }
 
 /**
  * 截取字符串
  * 
  * @param str 字符串
  * @param start 开始
  * @return 结果
  */
 public static string substring(final string str, int start)
 {
  if (str == null)
  {
   return nullstr;
  }
 
  if (start < 0)
  {
   start = str.length() + start;
  }
 
  if (start < 0)
  {
   start = 0;
  }
  if (start > str.length())
  {
   return nullstr;
  }
 
  return str.substring(start);
 }
 
 /**
  * 截取字符串
  * 
  * @param str 字符串
  * @param start 开始
  * @param end 结束
  * @return 结果
  */
 public static string substring(final string str, int start, int end)
 {
  if (str == null)
  {
   return nullstr;
  }
 
  if (end < 0)
  {
   end = str.length() + end;
  }
  if (start < 0)
  {
   start = str.length() + start;
  }
 
  if (end > str.length())
  {
   end = str.length();
  }
 
  if (start > end)
  {
   return nullstr;
  }
 
  if (start < 0)
  {
   start = 0;
  }
  if (end < 0)
  {
   end = 0;
  }
 
  return str.substring(start, end);
 }
 
 /**
  * 格式化文本, {} 表示占位符<br>
  * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
  * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
  * 例:<br>
  * 通常使用:format("this is {} for {}", "a", "b") - this is a for b<br>
  * 转义{}: format("this is \\{} for {}", "a", "b") - this is \{} for a<br>
  * 转义\: format("this is \\\\{} for {}", "a", "b") - this is \a for b<br>
  * 
  * @param template 文本模板,被替换的部分用 {} 表示
  * @param params 参数值
  * @return 格式化后的文本
  */
 public static string format(string template, object... params)
 {
  if (isempty(params) || isempty(template))
  {
   return template;
  }
  return strformatter.format(template, params);
 } 
 
 /**
  * 是否包含字符串
  * 
  * @param str 验证字符串
  * @param strs 字符串组
  * @return 包含返回true
  */
 public static boolean instringignorecase(string str, string... strs)
 {
  if (str != null && strs != null)
  {
   for (string s : strs)
   {
    if (str.equalsignorecase(trim(s)))
    {
     return true;
    }
   }
  }
  return false;
 }
 
 /**
  * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:hello_world- helloworld
  * 
  * @param name 转换前的下划线大写方式命名的字符串
  * @return 转换后的驼峰式命名的字符串
  */
 public static string converttocamelcase(string name)
 {
  stringbuilder result = new stringbuilder();
  // 快速检查
  if (name == null || name.isempty())
  {
   // 没必要转换
   return "";
  }
  else if (!name.contains("_"))
  {
   // 不含下划线,仅将首字母大写
   return name.substring(0, 1).touppercase() + name.substring(1);
  }
  // 用下划线将原始字符串分割
  string[] camels = name.split("_");
  for (string camel : camels)
  {
   // 跳过原始字符串中开头、结尾的下换线或双重下划线
   if (camel.isempty())
   {
    continue;
   }
   // 首字母大写
   result.append(camel.substring(0, 1).touppercase());
   result.append(camel.substring(1).tolowercase());
  }
  return result.tostring();
 }
}

补充知识:java list转string去掉[ 、]、空格

我就废话不多说了,大家还是直接看代码吧~

 public static void main(string[] args) {
  string s = "[aasa, bbbbb]";
  string s1 = s.replaceall("(?:\\[|null|\\]| +)", "");
 
  system.out.println(s1);
 }

在list<string> tostring后,会发现两边有[],且中间还有有空格产生。

解决办法如上代码所示,使用replaceall正则匹配 [、]、空格。

以上这篇java list集合返回值去掉中括号('[ ]')的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网