首页>代码>java实现简单的四则运算>/calculate/src/com/lyj/calculate/MyString.java
package com.lyj.calculate;
import java.util.ArrayList;
import java.util.List;

/**
 * 字符串测试类
 * @author LYJ
 *
 */
public final class MyString {
	
	
	/**
	 * 字符串转为大写
	 * @param source
	 * @return
	 */
	public  static final  String toUpCase(final String source){
		char []ch=source.toCharArray();
		int index=0;
		for(char c:ch){
			if(isAlphas(c)&&isSamll(c)){
				ch[index]=(char)(c-0x20);
			}
	        	index++;
			
		}
		return charToString(ch);
		
	}
	/**
	 * 字符串转化为小写
	 * @param source
	 * @return
	 */
	public static final String toLowCase( final String source){
		char []ch=source.toCharArray();
		int index=0;
		for(char c:ch){
			if(isAlphas(c)&&!isSamll(c)){
				ch[index]=(char) (c+0X20);
			
			}
		    index++;
			
		}
		return charToString(ch);
		
	}
	
	/**
	 * 分割字符串
	 * @param source
	 * @param split  分割串
	 * @return
	 */
	public final static String []split(String source,String split){
		List<String> string=new ArrayList<String>();
		int index=index(source.toCharArray(), split.toCharArray());
		if(index!=-1)
		  do{
			  string.add(source.substring(0, index));
			  source=source.substring(index+split.length()+1, source.length());
			  index=index(source.toCharArray(), split.toCharArray());
		  }
		  while(index(source.toCharArray(), split.toCharArray())!=-1);
		  string.add(source);
		
		
		return string.toArray(new String[string.size()]) ;
	}
	/***
	 * 分割字符串
	 * @param source
	 * @param split
	 * @return
	 */
	
	public final static String []split(String source,Character split){
		List<String> splitList=new ArrayList<String>();
		int start=-1;
		char[] sc=source.toCharArray();
		 for(int index=0,len=sc.length;index<len;index++){
			 if(sc[index]==split){
				 splitList.add(source.substring(start+1, index));
				 start=index;
			 }
		 }
		 splitList.add(source.substring(start+1,source.length()));
		 
		return splitList.toArray(new String[splitList.size()]);
	}
	

	/**
	 * 
	 * @param source
	 * @param old
	 * @param replace
	 * @return
	 */
	public final static String replace( final String source,Character old,String replace){
		int start=-1;
		final StringBuilder sbd=new StringBuilder(12);
		char[] sc=source.toCharArray();
		 for(int index=0,len=sc.length;index<len;index++){
			 if(sc[index]==old){
				 sbd.append(source.substring(start+1, index));
				 start=index;
				 sbd.append(replace);
			 }
		 }
		 sbd.append(source.substring(start+1,source.length()));
		
		
		return sbd.toString();
	}
	
	
	
	
	public final static String replaceAll(String source,String old,String replace){
		 final StringBuilder sd=new StringBuilder();
			int index=index(source.toCharArray(), old.toCharArray());
		if(index!=-1)
		  do{
			
			 sd.append(source.substring(0, index));
			 sd.append(replace);
			 source=source.substring(index+old.length(), source.length());
			 index=index(source.toCharArray(), old.toCharArray());
		  }
		  while(index!=-1);
		  sd.append(source);
		  
	    return sd.toString();
		  
	}
	
	/***
	 * 判断是否为字母
	 * @param c
	 * @return
	 */
	private static boolean isAlphas(char c){
		return (c>='a'&&c<='z')||(c>='A'&&c<='Z');
		
	}
	/**
	 * 判断是否为小写
	 * @param c
	 * @return
	 */
	private static boolean isSamll(char c){
		
		return (c>='a')&&(c<='z');
	}
	
	/**
	 * 判断字符串是否包含传入的字符串
	 * @param source 源字符串
	 * @param sequence 传入的字符串
	 * @return
	 */
	public   static final boolean contians(String source,String sequence){
		return containChary(source.toCharArray(), sequence.toCharArray());
	}
	
	
	public static final boolean contains(String source,Character ch){
		char [] sc=source.toCharArray();
		for(int start=0,len=sc.length,end=len-start-1;start<len;start++){
			if(sc[start]==ch||sc[end]==ch){
				return true;
			}
		}
		
		return false;
	}
	
    private final static String charToString(char ...ch){
	  final StringBuilder sbd=new StringBuilder();
		 for(char c:ch){
			 sbd.append(c);
		 }
		 return sbd.toString();
	 }
	
	 /**
	  * 判断一个char数组中是否包含另一个char数组的所有元素
	  * 并且是从数组的第一个元素对比至最后一个元素
	  * @param charAry
	  * @param newCharAyr
	  * @return
	  */
	 private final  static boolean containChary(char[]charAry,char[]newCharAyy){
		 int old_size=charAry.length;
		 int new_size=newCharAyy.length;
		 if(old_size<new_size){
			 return false;
		 }
		 int index=0,count=0,newIndex=0;
	    for(;index<old_size-2;index++){
		  while(newIndex<new_size-1&&charAry[index]==
			 newCharAyy[newIndex]&&charAry[index+1]==newCharAyy[newIndex+1] )
		  {
			  index++;
			  newIndex++;
			  count++;
			  if(count==new_size-1){
				  return true;
			  }
		  }
		  count=0;
		  newIndex=0;
		  
		  
		  
	  }
		 
		 return false;
		 
	 }
	 /**
	  * 找到要查找的字符串出现的位置
	  * @param source
	  * @param sequence
	  * @return 未找到返回不存在
	  */
	 public static int index(char[] source,char[] sequence){
		 int old_size=source.length;
		 int new_size=sequence.length;
		 int index=0,count=0,newIndex=0;
	    for(;index<old_size-2;index++){
	    	int position=index;
		  while(newIndex<new_size-1&&source[index]==
			  sequence[newIndex]&&source[index+1]==sequence[newIndex+1] )
		  {
			  index++;
			  newIndex++;
			  count++;
			  if(count==new_size-1){
				  return position;
			  }
		  }
		  count=0;
		  newIndex=0;
	  }
		 return -1;
		 
	 }
	 
	 public static int indexOf(String source,char ch,int index){
		 int count=1;
		 for(int i=0,len=source.length();i<len;i++){
			 if(source.charAt(i)==ch){
				 count++;
			 }
			 if(count==index+1){
				 return i;
			 }
		 }
		 
		 return -1;
	 }
	 
	 
	
	 
	
		 /**
		  * 找到要查找的字符串在被查找中的字符串出现的索引
		  * @param source
		  * @param find
		  * @return
		  */
		 public static int indexOf(String source,String find,int index){
			 char []sc=source.toCharArray();
			 int scIndex=0;
			 int count=0;
			 int findLen=find.length();
			 char first=find.charAt(0);
			 while(scIndex<sc.length-1){
				 char  s=sc[scIndex];
				 if(findLen==1){
					 if(s==first){
						 count++;
					 }
				 }
				 else  if(s==first&&source.substring(scIndex,scIndex+findLen).equals(find)){
					 count++;
				 }
				 if(count==index){
					 return scIndex;
				 }
				 scIndex++;
				 
			 }
			 return -1;
			 
		 }
	 
	

	
	
	

}
最近下载更多
年年有风  LV1 2022年3月7日
Bsword  LV2 2022年2月16日
1825900113  LV2 2021年12月30日
大兴西北  LV2 2020年12月13日
18259242926  LV1 2020年7月26日
wsk588  LV26 2019年12月18日
chenhuahao  LV18 2019年12月17日
忠于生物  LV1 2019年7月11日
xuzijian  LV1 2019年5月15日
光道一  LV2 2019年3月25日
最近浏览更多
ClydeSon  LV5 2023年12月27日
四十四十  LV3 2023年10月18日
别以为长得帅就不打你  LV5 2023年6月8日
wuziayng1232  LV10 2023年2月20日
heqian  LV16 2022年12月2日
九千风云  LV1 2022年10月10日
wadadd  LV7 2022年9月5日
林间听风  LV10 2022年6月27日
2213795250  LV3 2022年3月22日
年年有风  LV1 2022年3月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友