【C/Java/Dart/Go】处理文件名不能包含字符\/:*?“<>|
本帖最后由 dadashuai 于 2021-8-27 08:55 编辑## C语言
```
//检测文件名是否合法,不包含:\/:*?“<>|
int CheckFileName(char *path) {
int i = 0;
for(i = strlen(path) - 1; i >= 0; i--){
if(path == '\\' || path == '/' || path == ':' || path == '*' || path == '?' || path == '"' || path == '<' || path == '>' || path == '|'){
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 == '\\' || path == '/' || path == ':' || path == '*' || path == '?' || path == '"' || path == '<' || path == '>' || path == '|'){
} else {
buffer = path;
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 == "\\" || path=="/" || path==":"|| path == "*" || path=="?"||path=="\"" || path=="<" || path==">" || path=="|"){
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 == "\\" || path=="/" || path==":"|| path == "*" || path=="?"||path=="\"" || path=="<" || path==">" || path=="|"){
}else{
buffer.write(path);
}
}
return buffer.toString();
}
```
## go
```
//检测文件名是否合法,不包含:\/:*?“<>|
func CheckFileName(path string) bool {
var i = 0
for i = len(path) - 1; i >= 0; i-- {
if path == '\\' || path == '/' || path == ':' || path == '*' || path == '?' || path == '"' || path == '<' || path == '>' || path == '|' {
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 == '\\' || path == '/' || path == ':' || path == '*' || path == '?' || path == '"' || path == '<' || path == '>' || path == '|' {
} else {
buffer.WriteByte(path)
}
}
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;
}
```
用正则会更方便 有JS的吗 怎么使用(゚o゚; 905407220 发表于 2021-8-26 16:45
有JS的吗
加上了。js稍微修改一下就行了
页:
[1]