js中类似explode的方法

js的缺点之一就是函数库太少,自己搜了很多函数,其中这个explode很有用,在java也好,php也好,都有类似的分割字符串为数组的方法,不过要是想在js中用,那么只能自己手写了。

下面这个函数是较好用的一个:

function explode(inputstring, separators, includeEmpties) {
 
 inputstring = new String(inputstring);
  separators = new String(separators);

  if(separators == "undefined") {
    separators = " :;";
  }

  fixedExplode = new Array(1);
  currentElement = "";
  count = 0;

  for(x=0; x < inputstring.length; x++) {
    str = inputstring.charAt(x);
    if(separators.indexOf(str) != -1) {
        if ( ( (includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) {
        }
        else {
            fixedExplode[count] = currentElement;
            count++;
            currentElement = "";
        }
    }
    else {
        currentElement += str;
    }
  }

  if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
      fixedExplode[count] = currentElement;
  }
  return fixedExplode;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Hiveexplode函数可以将一个array或者map展开,生成多行数据。当使用explode(array)时,结果的每个元素都会生成一行数据;当使用explode(map)时,结果的每一对元素都会生成一行数据,其key为一列,value为一列。通常情况下,可以直接使用explode函数,但在某些情况下需要结合lateral view使用。 以下是一个示例查询,展示了如何使用explode函数: ``` select class, student_name, student_score from default.class_info lateral view explode(split(student, ',')) sn as student_name lateral view explode(split(score, ',')) sc as student_score ``` 这个查询将class_info表的student和score列进行了展开,生成了多行数据,其每一行包含了class、student_name和student_score三列。 需要注意的是,有一些限制条件需要遵守: 1. 在SELECT语句,不允许使用其他表达式。 2. 不支持嵌套使用UDTF(User-Defined Table-Generating Function)。 3. 不支持在GROUP BY / CLUSTER BY / DISTRIBUTE BY / SORT BY子句使用explode函数。 希望这个解答对您有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [hive explode](https://blog.csdn.net/zhouyan8603/article/details/82422604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Hiveexplode使用全解(附图片详解)](https://blog.csdn.net/weixin_40983094/article/details/110172155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值