1、判断字符串1中有几个字符串2:
/**
* 判断str1中包含str2的个数
*@param str1
* @param str2
* @return counter
*/
public static int countStr(String str1, String str2) {
int counter=0;
if (str1.indexOf(str2) == -1) {
return 0;
}
while(str1.indexOf(str2)!=-1){
counter++;
str1=str1.substring(str1.indexOf(str2)+str2.length());
}
return counter;
}
2、文件重命名(格式为:文档.txt-->文档(2).txt-->文档(3).txt):
/**
* 该方法用来文件重命名
* @param renameFile:要进行重命名的文件
* **/
private File renameFile(File renameFile){
//该文件在指定的文件夹中不存在了,则退出循环
int count=1;
String filePath = "";//文件路径
String fileName="";//文件名称
int index=0;
while(renameFile.exists()){
count++;
fileName = renameFile.getName();
index = fileName.lastIndexOf(".");
filePath = renameFile.getParent()+File.separator+fileName.substring(0,index)+"("+count+")."+fileName.substring(index+1);
renameFile = new File(filePath);
}
return renameFile;
}
3、使用ant打zip压缩包:
/**
* 打压缩包
* @param srcPathName:需要打成压缩包的文件夹路径
* @param zipFile:压缩包名称(路径+名称+.+后缀名)
* */
public void compress(String srcPathName,File zipFile) {
File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!");
}
if(zipFile.exists()){//说明当前目录下存在同名压缩文件
zipFile.delete();
}
Project prj = new Project();
Zip zip = new Zip();
zip.setEncoding("GBK");//设置编码,防止压缩文件名字乱码,还有被压缩文件的乱码
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
zip.addFileset(fileSet);
zip.execute(); //执行生成
}
4、使用ant解zip压缩包:
/**
* 解压指定zip文件
* @param unZipfile:需要解压缩的压缩包路径(路径+名称+.+后缀名)
* @param destFile:解压到的目录
* */
public void UNCompress(File unZipfile, File destFile) {
FileOutputStream fileOut;
File file;
InputStream inputStream;
ZipFile zipFile=null;
ZipOutputStream zipOut=null; //压缩Zip
try {
//生成一个zip的文件
zipFile = new ZipFile(unZipfile,"GBK");//设置编码,避免出现中文的情况(不管是压缩文件为中文,还是压缩包中的文件为中文)
//遍历zipFile中所有的实体,并把他们解压出来
for (@SuppressWarnings("unchecked")
Enumeration<ZipEntry> entries = zipFile.getEntries(); entries.hasMoreElements();) {
ZipEntry entry = entries.nextElement();
//生成他们解压后的一个文件
file = new File(destFile+File.separator+entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
File parent = file.getParentFile();// 如果指定文件的目录不存在,就创建.
if (!parent.exists()) {
parent.mkdirs();
}
//获取出该压缩实体的输入流
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(file);
int length = 0;
//将实体写到本地文件中去
while ((length = inputStream.read(buf)) > 0) {
fileOut.write(buf, 0, length);
}
fileOut.close();
inputStream.close();
}
}
zipFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
5、判断是否有敏感词汇:
//该方法用来判断是否有敏感词汇 //true:说明有敏感词汇 //false:说明没有敏感词汇 function filterKeywordMethod(value){ var result = false; //敏感词汇 var keywords=["select","insert","update","delete","create","alter", "drop","database","table","column","dbo","sys_","field", 'exec','execute','value','values']; //遍历敏感词数组 for(var i=0;i<keywords.length;i++){ //判断内容中是否包括敏感词 if(value.indexOf(keywords[i])!=-1){ result = true; break;//只要有一个直接返回 } } // alert("敏感值:"+value+"\n结果:"+result); return result; }6、判断是否有特殊字符:
//该方法用来判断字符串中是否存在特殊字符 //true:说明有特殊字符 //false:说明没有特殊字符 function checkStr(str){ var myReg = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\]./<>?~!@#¥……&*()——|{}【】‘;:”“'。,、?%+_]"); if(myReg.test(str)) return true; return false; }