Tea9源码网 - 免费分享精品源码、教程、软件|全网干货资源收集,免费下载。
  • 首页
  • 其它源码
  • 使用C/Java/javascrip/Dart/Go语言分别处理文件名不能包含 \/:*?“<>| 字符

使用C/Java/javascrip/Dart/Go语言分别处理文件名不能包含 \/:*?“<>| 字符

发布:吾爱破解论坛2021-8-30 16:54分类: 其它源码 标签: javascrip C语言 编程技巧 JAVA Dart Go语言

C语言

//检测文件名是否合法,不包含:\/:*?“<>|
int CheckFileName(char *path) {
        int i = 0;
        for(i = strlen(path) - 1; i >= 0; i--){
                if(path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|'){
                        return 0;
                }

        }
        return 1;
}
//去除文件名中不合法的部分 需要释放内存
char *HandleFileName(char *path) {
    size_t len = strlen(path)+1;
        char *buffer = (char *)malloc(len);
        int i = 0;
    int index = 0;
    memset(buffer, 0, len);
        for(i = 0; i < len; i++){
                if(path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|'){

                } else {
                        buffer[index] = path[i];
            index++;
                }

        }
        return buffer;
}

java

// 检测文件名是否合法,不包含:\/:*?“<>|
        static boolean CheckFileName(String path) {
                if (path == null)
                        return false;
                for (int i = path.length() - 1; i >= 0; i--) {
                        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                                        || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                                        || path.charAt(i) == '|') {
                                return false;
                        }

                }
                return true;
        }

        // 去除文件名中不合法的部分
        static String HandleFileName(String path) {
                if (path == null)
                        return "";
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < path.length(); i++) {
                        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                                        || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                                        || path.charAt(i) == '|') {

                        } else {
                                buffer.append(path.charAt(i));
                        }

                }
                return buffer.toString();
        }

dart

 //检测文件名是否合法,不包含:\/:*?“<>|
  static bool CheckFileName(String path){
if(path == null)return false;
    for(var i=path.length-1; i>=0; i--){
      if(path[i] == "\\" || path[i]=="/" || path[i]==":"|| path[i] == "*" || path[i]=="?"||path[i]=="\"" || path[i]=="<" || path[i]==">" || path[i]=="|"){
        return false;
      }

    }
    return true;
  }

  //去除文件名中不合法的部分
  static String HandleFileName(String path){
    if(path == null)return "";
    StringBuffer buffer = new StringBuffer();
    for(var i=0; i<path.length; i++){
      if(path[i] == "\\" || path[i]=="/" || path[i]==":"|| path[i] == "*" || path[i]=="?"||path[i]=="\"" || path[i]=="<" || path[i]==">" || path[i]=="|"){

      }else{
        buffer.write(path[i]);
      }

    }
    return buffer.toString();
  }

go

//检测文件名是否合法,不包含:\/:*?“<>|
func CheckFileName(path string) bool {
        var i = 0
        for i = len(path) - 1; i >= 0; i-- {
                if path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|' {
                        return false
                }

        }
        return true
}

//去除文件名中不合法的部分
func HandleFileName(path string) string {
        var buffer bytes.Buffer
        var i = 0
        for i = 0; i < len(path); i++ {
                if path[i] == '\\' || path[i] == '/' || path[i] == ':' || path[i] == '*' || path[i] == '?' || path[i] == '"' || path[i] == '<' || path[i] == '>' || path[i] == '|' {

                } else {
                        buffer.WriteByte(path[i])
                }

        }
        return buffer.String()
}

JavaScript

// 检测文件名是否合法,不包含:\/:*?“<>|
 function CheckFileName( path) {
    if (path == null)
        return false;
    for (var i = path.length - 1; i >= 0; i--) {
        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                || path.charAt(i) == '|') {
            return false;
        }
    }
    return true;
}
// 去除文件名中不合法的部分
 function HandleFileName( path) {
    if (path == null)
        return "";
    var buffer = ""
    for (var i = 0; i < path.length; i++) {
        if (path.charAt(i) == '\\' || path.charAt(i) == '/' || path.charAt(i) == ':' || path.charAt(i) == '*'
                || path.charAt(i) == '?' || path.charAt(i) == '\"' || path.charAt(i) == '<' || path.charAt(i) == '>'
                || path.charAt(i) == '|') {
        } else {
            buffer += (path.charAt(i));
        }
    }
    return buffer;
}
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

已有 2986 人阅读