本帖最后由 dadashuai 于 2021-8-27 08:55 编辑
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;
}
|