java-工具类(校验整数、返回指定字符在某字符串中出现的次数)

作者在 2019-03-23 21:13:40 发布以下内容
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class StringUtil {
	static int varlen1;
	static int varlen2;
	
	static String varstr1;
	static String varstr2;
	static String varstr3;
	private static Map<String,String> mapMonth =null;
	
	static{
		mapMonth = new HashMap<>();
		mapMonth.put("Jan", "01");
		mapMonth.put("Feb", "02");
		mapMonth.put("Mar", "03");
		mapMonth.put("Apr", "04");
		mapMonth.put("May", "05");
		mapMonth.put("Jun", "06");
		mapMonth.put("Jul", "07");
		mapMonth.put("Aug", "08");
		mapMonth.put("Sep", "09");
		mapMonth.put("Oct", "10");
		mapMonth.put("Nov", "11");
		mapMonth.put("Dec", "12");
	}
	public static String[] getAllTtype(){
		return allTtype;
	}
	
	/**
	 * 返回sourceStr 可以被indexStr截取的字符串个数
	 * @param sourceStr
	 * @param indexStr
	 * @return
	 */
	public static int indexCounts(String sourceStr,String indexStr){
		
		varlen1 = sourceStr.length();
		varlen2 = indexStr.length();
		
		int i=0;//字符串下标
		int n=0;//某字符串在指定字符串中的个数
		
		while (i< varlen1){
			varstr1 = sourceStr.substring(i, i);//substr(sourceStr,i,1);
        	varstr2 = indexStr.substring(1,1);//获取指定用于截串的字符串的第一个字符
        	
        	if(varstr1.equals(varstr2)){
        		if(i+varlen2<=varlen1){//判断字符下标
        			if(indexStr.equals(sourceStr.substring(i, i+varlen2))){
            			n++;
            			i=i+varlen2;
            		}
        		}
        	}
        	i++;
		}
		return n+1;
	}
	
	/**
	 * 返回indexStr出现的次数
	 * @param sourceStr
	 * @param indexStr
	 * @return
	 */
	public static int indexStrCounts(String sourceStr,String indexStr){
		
		varlen1 = sourceStr.length();
		varlen2 = indexStr.length();
		
		int i=0;//字符串下标
		int n=0;//某字符串在指定字符串中的个数
		
		while (i< varlen1){
			varstr1 = sourceStr.substring(i, i);//substr(sourceStr,i,1);
        	varstr2 = indexStr.substring(1,1);//获取指定用于截串的字符串的第一个字符
        	
        	if(varstr1.equals(varstr2)){
        		if(i+varlen2<=varlen1){//判断字符下标
        			if(indexStr.equals(sourceStr.substring(i, i+varlen2))){
            			n++;
            			i=i+varlen2;
            		}
        		}
        	}
        	i++;
		}
		return n;
	}
	
	/** 是否是整数
	 * 10.001 false
	 * 10.00 true
	 * @param strNum
	 * @return
	 */
	public static boolean isNumeric(String strNum){
	    
	    boolean boo = strRegular(strNum,0,9);
	    int cou = indexCounts(strNum,"."); 
	    if(boo){
	    	return boo;
	    }else{
	    	if(cou==1){
		    	return false;
		    }else if(cou==2){
		    	String endNum = indexOf(strNum, ".", 2);
		    	boolean bo = strRegular(endNum,0,9);
		    	if(bo){
		    		Integer in = new Integer(endNum);
		    		if(in==0){
		    			return true;
		    		}else{
		    			return false;
		    		}
		    	}else{
		    		return false;
		    	}
		    	
		    }else{
		    	return false;
		    }
	    }
	    
	    /*if(!boo){
	    	
	    }*/
	   // return pattern.matcher(strNum).matches();   
	}
	
	public static boolean strRegular(String strNum,int startNum,int endNum){
		 Pattern pattern = Pattern.compile("^-?["+startNum+"-"+endNum+"]*");
		return pattern.matcher(strNum).matches();
	}  }
java | 阅读 2106 次
文章评论,共0条
游客请输入验证码