首页 > 试题广场 >

字符串替换(测试开发)

[编程题]字符串替换(测试开发)
  • 热度指数:180 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
起点客户端上允许用户对作品进行评论,为了防止用户恶意评论,发表不当内容,需要对用户发布的内容进行过滤,请写程序过滤用户发布内容中带有的QQ号(6~10位数字组成)
允许对内容严格操作,如用户发表了 作者大大666666,为你点赞 ,经过过滤后也可以为作者大大,为你点赞 ,将666666过滤掉了。
示例1

输入

"作者大大666666666,为你点赞"

输出

"作者大大,为你点赞"

说明


备注:
QQ号显然是不能以0开头的,请注意
python 字符串处理:
class Solution:
def filterContent(self , content ):
# write code here
import re
r='[1-9]\d{5,9}'
s1=re.split(r,content)
if re.findall(r,content)==[]:
    return content
else:
    return (""''.join(s1))

编辑于 2021-02-24 21:54:09 回复(0)

go语言,用正则表达式

package main

import "regexp"

var re = regexp.MustCompile(`[1-9]\d{5,9}`)

/**
  * 过滤内容中出现的QQ号
  * @param content string字符串 待过滤内容
  * @return string字符串
*/
func filterContent(content string) string {
    // write code here
    return re.ReplaceAllString(content, "")
}
发表于 2021-02-05 15:47:22 回复(0)
class Solution:
    def filterContent(self , content ):
        # write code here
        import re
        # 使用正则findall方法,查找出字符串comment中的非0开头的6-10位数字,生成列表num中
        num = re.findall('[1-9][0-9]{5,9}', content)
        # 遍历列表num中的各个号码
        for i in num:
            # 替换掉字符串comment中的号码
            content = content.replace(i, '')
        return content
编辑于 2023-03-02 14:02:30 回复(0)
import java.util.*;


public class Solution {
  public String filterContent (String s) {
        String pattern = "[1-9][0-9]{5,9}";
        return s.replaceAll(pattern, "");
  }
}
发表于 2022-07-22 16:55:42 回复(0)
没有控制多个qq号重复情况
public static String filterContent (String content) {
        // write code here
        StringBuilder str = new StringBuilder();
        String c = content;
        for(int i = 0;i<content.length();i++){
            if(Character.isDigit(content.charAt(i))){
                if(str.length() ==0&& Integer.valueOf(content.substring(i,i+1))==0){
                    continue;
                }

                str.append(content.substring(i,i+1));
                if(i == content.length()-1){
                    if(str.length()<=10&&str.length()>=6){
                        c = c.replace(new String(str),"");
                    }
                }

            }else{
                if(str.length()<=10&&str.length()>=6){

                    c = c.replace(new String(str),"");
                }
                    str.delete(0,str.length());


            }

        }
        return c;
    }





编辑于 2021-01-24 14:04:47 回复(0)