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

📄 stringhelper.java

📁 国外的一套开源CRM
💻 JAVA
字号:
/*
 * 
 * Copyright (c) 2004 SourceTap - www.sourcetap.com
 *
 *  The contents of this file are subject to the SourceTap Public License 
 * ("License"); You may not use this file except in compliance with the 
 * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 */

package com.sourcetap.sfa.util;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;

import org.ofbiz.base.util.Debug;


/**
 * DOCUMENT ME!
 *
 */
public class StringHelper {
	public static final String module = StringHelper.class.getName();

    /**
     * DOCUMENT ME!
     *
     * @param origString 
     * @param removeString 
     * @param insertString 
     *
     * @return 
     */
    public static String replaceAll(String origString, String removeString,
        String insertString) {
        Debug.logVerbose("[StringHelper.replaceAll] Args: " + origString +
                "/" + removeString + "/" + insertString, module);

        StringBuffer buf = new StringBuffer(origString);

        int loopCounter = 0;
        int completePos = 0;
        int replaceStartPos = buf.toString().indexOf(removeString, completePos);
        int replaceEndPos = 0;

        while (replaceStartPos > 0) {

            // Calculate the position of the first character NOT to be replaced after the replace start position.
            replaceEndPos = replaceStartPos + removeString.length();

            // Perform the string replacement.
            buf.replace(replaceStartPos, replaceEndPos, insertString);

            // Figure out where to start the search from next time through the loop.  This will be the replace start position
            // plus the length of the insert string.
            completePos = replaceStartPos + insertString.length();

            // Figure out the replace start position for next time through.
            replaceStartPos = buf.toString().indexOf(removeString, completePos);

            if (loopCounter++ > 1000) {
                Debug.logWarning("[StringHelper.replaceAll] Loop detected.", module);

                return buf.toString();
            }
        }

        Debug.logVerbose(
                "[StringHelper.replaceAll] String after replacement: " +
                buf.toString(), module);

        return buf.toString();
    }
    
    public static String valueOf( Object o)
    {
    	if ( o == null )
    		return "";
    	else
    		return String.valueOf( o);
    }
    
    
	/** 
	 * Converts a Java variable name to a field description.
	 * The naming conventions used to allow for this are as follows: a Java name (ejb or field) is in all 
	 * lower case letters, except the letter at the beginning of each word (for example: 
	 * NeatEntityName or RandomFieldName). 
	 * @param javaName The Java variable name
	 * @return The description
	 */
	public static String javaNameToDescription(String javaName) {
		if (javaName == null) return null;
		if (javaName.length() <= 0) return "";
		StringBuffer description = new StringBuffer();

		description.append(Character.toUpperCase(javaName.charAt(0)));
		int namePos = 1;

		while (namePos < javaName.length()) {
			char curChar = javaName.charAt(namePos);

			if (Character.isUpperCase(curChar)) description.append(' ');
			description.append(curChar);
			namePos++;
		}

		return description.toString();
	}
	
		/**
		 * Converts text to HTML.  Replace new lines in text with BR tags 
		 * and replace leading white space with html space characters
		 * @param str - String containing the plain text.
		 * @return the HTML verion of the string
		 */
		public final static String textToHtml(String str) {
			
			if ( str == null )
				return str;
				
			StringBuffer buf = new StringBuffer();
			int len = str.length();
			boolean newline = true;
			for (int i = 0; i < len; i++) 
			{
				char c = str.charAt(i);
				if (c == '\n')
				{
					buf.append("<br>");
					newline = true;
				}
				else if ( (c == ' ') && newline )
				{
					buf.append("&nbsp;");
				}
				else
				{
					newline = false;
					buf.append(c);
				}
			}

			return buf.toString();
	
		}

	public static String formatDate( Date date )
	{
		if (date == null)
			 return "";
		 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		 return df.format(date);        

	}
	
	public static String convertDate( String inDate )
	{
		StringTokenizer st = new StringTokenizer(inDate, " ");
		if ( st.countTokens() != 3)
			st = new StringTokenizer(inDate, "/");
			
		if ( st.countTokens() != 3 )
			return inDate;
			
		String month = st.nextToken();
		String day   = st.nextToken();
		String year  = st.nextToken();
		
		try {
			int year4 = Integer.parseInt(year);
			
			if ( ( year4 > 4 ) && ( year4 < 100) )
				year4 = year4 + 1900;
			else if ( year4 < 100  )
				year4 = year4 + 2000;
			
			year = String.valueOf(year4);
			return month + " " + day + " " + year;
		}
		catch (Exception e)
		{
			return inDate;
		}
	}
	public static ArrayList splitCityStateZip(String input)
	{
		ArrayList retList = new ArrayList();
		retList.add(null);
		retList.add(null);
		retList.add(null);
		retList.add(null);
		
		try {
			ArrayList flds = new ArrayList();
			StringTokenizer st = new StringTokenizer(input, " ");
			while (st.hasMoreTokens()) {
					 flds.add(st.nextToken());
			}
			int numFlds = flds.size();
			if ( numFlds < 3)
			{
				throw new IllegalArgumentException("field is not city state zip: " + input);
			}
			int pos = numFlds - 1;
			String zip = (String) flds.get(pos);
			if ( (zip.length() < 5) && ( pos > 0 ) )
			{
				pos--;
				zip = (String) flds.get(pos) + "-" + zip; 
			}
			String s = zip.replaceAll("-", "");
			int tstInt = Integer.parseInt(s); // if not an int, then an exception will be thrown
				
			String state = null;
			if ( pos > 0 )
			{
				pos--;
				state = (String) flds.get(pos);
				if ( state.length() > 2)
				{
					Debug.logVerbose("State is not 2 characters: " + state, module);
					throw new IllegalArgumentException("Invalid State Format");
				}
			}
				
			String city = (String) flds.get(0);
			for ( int i = 1; i < pos; i++)
			{
				city = city + " " + (String) flds.get(i);
			}
				
			retList.set(0, city);
			retList.set(1, state);
			retList.set(2, zip);
			retList.set(3, "USA");				
		}
		catch( Exception e)
		{
			Debug.logVerbose("unable to transform address field:" + input + ": " + e.getMessage(), module);
			retList.set(0, input);
		}
		
		return retList;
	}

}

⌨️ 快捷键说明

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