zoukankan      html  css  js  c++  java
  • 替换掉字符串中重复的词(只保留一个)

                private string allKeywords = "";
            public string outputKeywords(Model.users users)
            {
                allKeywords += (" "+keywordsOfAge(users.age));
                allKeywords += (" " + keywordsOfSex(users.sex));
                allKeywords += (" " + keywordsOfHightAndWeight(users.hight, users.weight, users.sex));
                allKeywords += (" " + keywordsOfJob(users.job));
                allKeywords += (" " + keywordsOfyylx(users.yylx));
                string[] jj = keywordsOfjj(users.jj).Split(' ');
                for(int i =0;i<jj.Length;i++)
                {
                    allKeywords = allKeywords.Replace(jj[i],"");
                }

          

    //allKeywords中是有可能包含重复词的字符串,而且词之间以空格隔开

                allKeywords = Regex.Replace(allKeywords, "\\s+", " ");//替换掉词之间多余的空格(多于1个空格的替换成一个)

                //开始替换掉重复的关键字
                string[] allKeywords2 =allKeywords.Split(' ');
                ArrayList al = new ArrayList();
                for (int i = 0; i < allKeywords2.Length; i++)
                {
                    if (!al.Contains(allKeywords2[i]))
                    {
                        al.Add(allKeywords2[i]);
                    }
                }
                allKeywords = "";//清理原来的字符串
                for (int i = 0; i < al.Count; i++)
                {
                    allKeywords += al[i] + " ";
                }
                return allKeywords;
            }

    暂时没想到更简洁的方法

               

  • 相关阅读:
    前端使用crypto.js进行加密
    C#编程总结(七)数据加密——附源码
    PID file /run/zabbix/zabbix_server.pid not readable (yet?) after start. 报错解决
    TNS-12560: Message 12560 not found; No message file for product=network, facility=TNS报错
    oracle无法启动asm实例记录
    linux添加硬盘分区挂载教程
    Oracle Database 12c Release 2安装过程实录
    Centos6.9minimal版系统安装图形化界面
    扫描工具nmap介绍
    Zabbix系列之六——添加web监测
  • 原文地址:https://www.cnblogs.com/terrorpig/p/1350990.html
Copyright © 2011-2022 走看看