1. 程式人生 > >JAVA實現EXCEL公式專題(四)——字符串函數

JAVA實現EXCEL公式專題(四)——字符串函數

main 問題 int start boolean java ... ringbuf out substring

直接上代碼:
/**
 *  項目名稱:
 *  文件說明: ExCEL公式類型:字符串公式
 *  主要特點:
 *  版本:1.0
 *  制作人:劉晨曦
 *  創建時間:2013-12-3
 **/
package EXCEL;

/**
 * @author lcx
 *
 */
public class StringFunctions {

	public static String right(String str,int len)
	{
		if(len>str.length())
			throw new IllegalArgumentException("截取長度不能大於數組長度");
		return str.substring(str.length()-len, str.length());
	}

	public static String left(String str,int len)
	{
		if(len>str.length())
			throw new IllegalArgumentException("截取長度不能大於數組長度");
		return str.substring(0, len);
	}


	public static String mid(String str,int start,int len)
	{
		if(start+len-1>str.length())
			throw new IllegalArgumentException("截取越限");
		return str.substring(start-1, start+len-1);
	}

	public static int len(String str)
	{
		if(str==null)
			return 0;
		return str.length();
	}

	public static String concatenate(String...strs)
	{
		StringBuffer buffer=new StringBuffer();
		for(String str:strs)
			buffer.append(str);
				return buffer.toString();
	}

	public static boolean exact(String str1,String str2)
	{
		return str1.equals(str2);
	}

	public static String upper(String str1)
	{
		if(str1==null)
			return "";
		return str1.toUpperCase();
	}

	public static int find(String find_text,String within_text,int start_num)
	{
		if(start_num<=0||start_num>within_text.length())
			throw new IllegalArgumentException("start_num參數輸入問題");
		if(find_text.length()>within_text.length())
			return -1;
		String tag=within_text.substring(start_num-1);
		int index=tag.indexOf(find_text);
		if(index<0)
			return -1;
		return index+start_num;
		
	}
	
	public static void main(String[] args) {
		
		/*******************測試字符串*****************************/

		System.out.println(mid("我想要編程",2,2));
		System.out.println(left("我想要編程",2));
		System.out.println(right("我想要編程",3));
		System.out.println(concatenate("a","b","c","d"));
		System.out.println(exact(new String("aaa"),new String("aaa")));
		System.out.println(upper("aaaa"));
		System.out.println(find("b","abcabc",3));
		
	}
}

JAVA實現EXCEL公式專題(四)——字符串函數