⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutils.java

📁 一种快速开发的Java Web架构,doc里有详细的设计文档和开发文档。
💻 JAVA
字号:
/**
 *
 * 功能	 Coarse Function Description
 * 类名  StringUtils
 *
 * ver       变更日        部门        变更者   变更内容
 * ──────────────────────────────────
 * V1.0    2006-10-25     国内事业部     陈志武     初版
 * V1.1    2006-10-25     国内事业部     陈志武     改定
 *
 */

package com.hisoft.cottonbusiness.core.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;

/**
 * Class description in detail
 * 
 * 
 * @author 陈志武
 * @version Ver 1.0 2006-10-25 改订
 * @since Ver 1.0
 */

public class StringUtils
{

	public static int getCount(String string, String s)
	{
		int count = 0;

		int pos = 0;
		for (int i = 0; i < string.length(); i = pos + 1)
		{
			pos = string.indexOf(s, i);
			if (pos != -1)
			{
				count++;
			}
			else
			{
				break;
			}
		}

		return count;
	}

	/**
	 * convert 2006-10-15 to 2006年10月15日
	 * 
	 * @param dateFormat
	 * @return
	 */
	public static String toDateCN(String date)
	{
		String dateCN = "";

		StringTokenizer token = new StringTokenizer(date, "-");

		if (token.hasMoreTokens())
			dateCN += token.nextToken() + "年";
		if (token.hasMoreTokens())
			dateCN += token.nextToken() + "月";
		if (token.hasMoreTokens())
			dateCN += token.nextToken() + "日";
		
		return dateCN;
	}

	public static String encodeString(String input)
	{
		String output = null;
		try
		{
			output = URLEncoder.encode(input, "UTF-8");
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}

		return output;
	}

	public static void convertToFile(String file, String destFile) throws IOException
	{
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
		Properties props = new Properties();

		props.load(is);

		Properties output = new Properties();
		OutputStream os = new FileOutputStream(destFile);
		Enumeration e = props.keys();
		System.out.println("now parse properties file: ");
		while (e.hasMoreElements())
		{
			String key = (String) e.nextElement();
			String value = props.getProperty(key);
			System.out.println("value:" + value);

			value = encodeString(value);
			output.put(key, value);
		}

		output.store(os, "converted file!");

		os.close();
	}

	public static String decodeString(String input)
	{
		String output = null;
		try
		{
			output = URLDecoder.decode(input, "UTF-8");
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}

		return output;
	}
	
	/**
	 * eg: cat=COTTONIMPORTEXPORT1&cat=COTTONIMPORTEXPORT2&cat=COTTONIMPORTEXPORT3&cat=COTTONIMPORTEXPORT4
	 * 
	 * @param queryString
	 * @param key eg: "&cat="
	 * @return
	 */
	public static String[] getParameterValues(String queryString,String key)
	{
		StringTokenizer tokenizer = new StringTokenizer("&" + queryString,key);
		String[] result = new String[tokenizer.countTokens()];
		int i = 0;
		while(tokenizer.hasMoreTokens())
		{
			String value = tokenizer.nextToken();
			result[i] = value;
			i++;
		}
		
		return result;
	}
	
	/**
	 *  convenient method for getting each token of some string
	 *  
	 * @param input
	 * @param delimiter
	 * @return
	 */
	public static String[] getStrings(String input,String delimiter)
	{
		StringTokenizer tokenizer = new StringTokenizer(input,delimiter);
		String[] result = new String[tokenizer.countTokens()];
		
		int i = 0;
		while(tokenizer.hasMoreTokens())
		{
			result[i] = tokenizer.nextToken();
			i++; 
		}
		
		return result;
	}
	
	/**
	 *  default delimeter is ","
	 *  
	 * @param input
	 * @return
	 */
	public static String[] getStrings(String input)
	{
		return getStrings(input,",");
	}	
	
	public static String[] mergeStrings(String[] first,String[] second)
	{
		String[] result = new String[first.length + second.length];
		
		for (int i = 0; i < first.length; i++)
		{
			result[i] = first[i];
		}
		
		for (int j = 0; j < second.length; j++)
		{
			result[j + first.length] = second[j];
		}
		
		return result;
	}
	

    public static  void assertParamValue(String[] input,String refer)
    {
    	if (null == input)
    	{
    		throw new RuntimeException("input can not be null" );
    	}

    	
    	for (int i = 0 ; i < input.length; i++)
    	{
    		if (null == refer)
    		{
    			if (null == input[i])
    			{
    				throw new RuntimeException("input[" + i + "]" + input[i] + " can not be null" );
    			}
    		}
    		else
    		{
    			if (refer.equals(input[i]))
    			{
    				throw new RuntimeException("input[" + i + "]" + input[i] + " can not be[\"" + refer + "\"]");
    			}
    		}
    	}
    }
	

	public static void main(String[] args) throws IOException
	{
//		String file = "parameter.properties";
//		String destFile = "E:/JavaProjects/cncottonQuery/config/parameter1.properties";
//
//		convertToFile(file, destFile);

//		String date = "2006-10-15";
//		
//		System.out.println(toDateCN(date));
		
//		String cat = "cat=COTTONIMPORTEXPORT1&cat=COTTONIMPORTEXPORT2&cat=COTTONIMPORTEXPORT3&cat=COTTONIMPORTEXPORT4";
//		String cat = "priceIndex=GAUZEIMPORTEXPORTIMP";
//		String[] result = getParameterValues(cat,"=");
//		String[] result = getStrings("aaa,bb,dd,ccc,",",");
//		String[] first = {"aa","bb","cc"};
//		String[] second = {"aa1","bb1","cc1"};
//		String[] m = mergeStrings(first,second);
		
//		System.out.println(m);
		
		String[] input = {"aa","bb",""};
		
		assertParamValue(input,"");
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -