java 字符串数组 操作

本文介绍了一个实用的Java工具类StringTool,该类提供了多种字符串操作方法,包括打印字符串数组、删除指定位置的字符串、移除特定值的所有实例以及去除数组中的重复项等。这些方法能有效帮助开发者简化字符串处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  public class StringTool {
  /**
  * 在控制台打印字符串数组 a
  * @param a
  */
  public static void printArray(String[] a){
  System.out.print("[");
  for(String s : a){
  System.out.print(s+",");
  }
  System.out.println(" size="+a.length+"]");
  }
  /**
  * 将字符数组中下标为idx的字符串从数组src中删除,idx从0开始
  * @param src
  * @param idx
  * @return 返回删除指定元素后的数组
  */
  public static String[] removeElement(String[] src ,int idx){
  int length = src.length;
  if(idx>=length){
  throw new IndexOutOfBoundsException();
  }
  String[] result = new String[src.length-1];
  for(int i = 0;iidx){
  src[i-1] = src[i];
  }
  }
  for(int j = 0;j字符串从数组src中删除,idx从0开始
  * @param src
  * @param index
  * @return 返回删除指定元素后的数组
  */
  public static String[] remove(String[] src ,int index){
  int length = src.length;
  if(index>=length){
  throw new IndexOutOfBoundsException();
  }
  String[] result = new String[length-1];
  System.arraycopy(src, 0, result, 0, index);
  System.arraycopy(src, index+1, result, index, length-index-1);
  return result;
  }
  /**
  * 删除src字符串数组中值与string元素相同的所有元素
  * @param src
  * @param string
  * @return
  */
  public static String[] remove(String[] src ,String string){
  if(string==null){
  for(int i = 0; i字符串数组中重复字符串,
  * 在两个重复的字符串中将先剔除第二次出现的字符串
  * @param src
  * @return 返回无重复元素的字符串数组
  */
  public static String[] removeRepeat(String[] src){
  for(int i = 0; i<src.length;i++){
  for(int j = i+1 ;j<src.length;j++){
  if(src[i]==null && src[i]==src[j]){
  src = remove(src,j);
  j--;
  }else if(src[i].equals(src[j])){
  src = remove(src,j);
  j--;
  }
  }
  }
  return src;
  }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值