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

📄 stringutil.java

📁 利用SyncML开发客户端程序的中间件
💻 JAVA
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
 
package com.funambol.util;

import java.util.Vector;

/**
 * Utility class useful when dealing with string objects.
 * This class is a collection of static functions, and the usage is:
 *
 * StringUtil.method()
 *
 * it is not allowed to create instances of this class
 */
public class StringUtil {
    
    private static final String HT = "\t";
    private static final String CRLF = "\r\n";

    
    // This class cannot be instantiated
    private StringUtil() { }

    /**
     * Split the string into an array of strings using one of the separator
     * in 'sep'.
     *
     * @param s the string to tokenize
     * @param sep a list of separator to use
     *
     * @return the array of tokens (an array of size 1 with the original
     *         string if no separator found)
     */
    public static String[] split(String s, String sep) {
        // convert a String s to an Array, the elements
        // are delimited by sep
        Vector tokenIndex = new Vector(10);
        int len = s.length();
        int i;
        
        // Find all characters in string matching one of the separators in 'sep'
        for (i = 0; i < len; i++) {
            if (sep.indexOf(s.charAt(i)) != -1 ){
                tokenIndex.addElement(new Integer(i));
            }
        }

        int size = tokenIndex.size();
        String[] elements = new String[size+1];

        // No separators: return the string as the first element
        if(size == 0) {
            elements[0] = s;
        }
        else {
            // Init indexes
            int start = 0;
            int end = ((Integer)tokenIndex.elementAt(0)).intValue();
            // Get the first token
            elements[0] = s.substring(start, end);

            // Get the mid tokens
            for (i=1; i<size; i++) {
                // update indexes
                start = ((Integer)tokenIndex.elementAt(i-1)).intValue()+1;
                end = ((Integer)tokenIndex.elementAt(i)).intValue();
                elements[i] = s.substring(start, end);
            }
            // Get last token
            start = ((Integer)tokenIndex.elementAt(i-1)).intValue()+1;
            elements[i] = (start < s.length()) ? s.substring(start) : "";
        }

        return elements;
    }
    
    /**
     * Split the string into an array of strings using one of the separator 
     * in 'sep'.
     *
     * @param list the string array to join
     * @param sep the separator to use
     *
     * @return the joined string
     */
    public static String join(String[] list, String sep) {
        StringBuffer buffer = new StringBuffer(list[0]);
        int len = list.length;

        for (int i = 1; i < len; i++) {
            buffer.append(sep).append(list[i]);
        }
        return buffer.toString();
    }

    /**
     * Returns the string array
     * @param stringVec the Vecrot of tring to convert
     * @return String []
     */
    public static String[] getStringArray(Vector stringVec){
        
        if(stringVec==null){
            return null;
        }
        
        String[] stringArray=new String[stringVec.size()];
        for(int i=0;i<stringVec.size();i++){
            stringArray[i]=(String) stringVec.elementAt(i);
        }
        return stringArray;
    }
    
    /**
     * Find two consecutive newlines in a string.
     * @param s - The string to search
     * @return int: the position of the empty line
     */
    public static int findEmptyLine(String s){
        int ret = 0; 
        
        // Find a newline
        while( (ret = s.indexOf("\n", ret)) != -1 ){
            // Skip carriage returns, if any
            while(s.charAt(ret) == '\r'){
                ret++;
            }
            if(s.charAt(ret) == '\n'){
                // Okay, it was empty
                ret ++;
                break;
            }
        }
        return ret;
    }
    
    /**
     * Removes unwanted blank characters
     * @param content
     * @return String
     */
    public static String removeBlanks(String content){

        if(content==null){              
            return null;
        }

        StringBuffer buff = new StringBuffer();
        buff.append(content);

        for(int i = buff.length()-1; i >= 0;i--){
            if(' ' == buff.charAt(i)){
                buff.deleteCharAt(i);
            }
        }
        return buff.toString();
    }
    
    /**
     * Removes unwanted backslashes characters
     *
     * @param content The string containing the backslashes to be removed
     * @return the content without backslashes
     */
    public static String removeBackslashes(String content){

        if (content == null) {              
            return null;
        }

        StringBuffer buff = new StringBuffer();
        buff.append(content);

        int len = buff.length();
        for (int i = len - 1; i >= 0; i--) {
            if ('\\' == buff.charAt(i))
                buff.deleteCharAt(i);
        }
        return buff.toString();
    }
    
    /**
     * Builds a list of the recipients email addresses each on a different line,
     * starting just from the second line with an HT ("\t") separator at the
     * head of the line. This is an implementation of the 'folding' concept from
     * the RFC 2822 (

⌨️ 快捷键说明

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