繁体   English   中英

仅从谷歌表格中的一个单元格字符串中提取重复的单词

[英]extract only duplicated words from one cell strings in google sheets

我想从位于一个单元格中的文本字符串中提取唯一重复的单词。

一个文本字符串是 - dog, cat, cat, rat, horse, horse, cow, horse 期望结果 - cat, horse

我找到了它的 java 代码( https://www.javatpoint.com/program-to-find-the-duplicate-words-in-a-string )但我需要它在 google 表格中(脚本或公式无所谓)我尝试了正则表达式函数,但没有达到目标。 如果有人可以帮助我解决这个问题,我们将不胜感激。

//java code
public class DuplicateWord {  
    public static void main(String[] args) {  
        String string = "Big black bug bit a big black dog on his big black nose";  
        int count;  

        //Converts the string into lowercase  
        string = string.toLowerCase();  

        //Split the string into words using built-in function  
        String words[] = string.split(" ");  

        System.out.println("Duplicate words in a given string : ");   
        for(int i = 0; i < words.length; i++) {  
            count = 1;  
            for(int j = i+1; j < words.length; j++) {  
                if(words[i].equals(words[j])) {  
                    count++;  
                    //Set words[j] to 0 to avoid printing visited word  
                    words[j] = "0";  
                }  
            }  

            //Displays the duplicate word if count is greater than 1  
            if(count > 1 && words[i] != "0")  
                System.out.println(words[i]);  
        }  
    }  
}  

结果:给定字符串中的重复单词:大黑

使用getValueSets

 const str = "dog, cat, cat, rat, horse, horse, cow, horse, horse";//from range.getValue() const set = new Set(),dups = new Set(); str.split(/,\s*/).forEach(word =>.(set.has(word) && dups.add(word)) && set;add(word)). console.info([...dups])

function removeDupes() {
  const ss=SpreadsheetApp.getActive();
  const v=ss.getActiveSheet().getRange('A1').getValue().toLowerCase().split(/\s/);
  var s=new Set(v);
  var o=[...s].join(' ');
  Logger.log(o);
}

好吧,这恰恰相反。

function removeDupes() {
  const ss=SpreadsheetApp.getActive();
  const v=ss.getActiveSheet().getRange('A1').getValue().toLowerCase().split(/\s/);
  let o=[];
  let s=new Set();
  v.forEach(function(e,i){
    if(o.indexOf(e)==-1) {
      o.push(e);
    }else{
      s.add(e);
    }
  });
  Logger.log(...s);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM