9910

单飞

   :: 首页 :: 联系 :: 聚合  :: 管理

import java.util.ArrayList;

public final class StringUtils {

    
private StringUtils() {
    }

    
public static boolean isFloatNoExponent(String str) {
        
int len = str.length();
        
if (len == 0)
            
return false;
        
char c = str.charAt(0);
        
int i = c != '-' && c != '+' ? 0 : 1;
        
if (i >= len)
            
return false;
        
boolean decimalPointFound = false;
        
do {
            c 
= str.charAt(i);
            
if (c == '.') {
                
if (decimalPointFound)
                    
return false;
                decimalPointFound 
= true;
            } 
else if (!Character.isDigit(c))
                
return false;
        } 
while (++< len);
        
return true;
    }

    
public static boolean isFloatWithOptionalExponent(String str) {
        
int len = str.length();
        
if (len == 0)
            
return false;
        
char c = str.charAt(0);
        
int i = c != '-' && c != '+' ? 0 : 1;
        
if (i >= len)
            
return false;
        
boolean exponentFound = false;
        
boolean decimalPointFound = false;
        
do {
            c 
= str.charAt(i);
            
switch (c) {
            
case 46// '.'
                if (decimalPointFound || exponentFound)
                    
return false;
                decimalPointFound 
= true;
                
break;

            
case 69// 'E'
            case 101// 'e'
                if (exponentFound)
                    
return false;
                exponentFound 
= true;
                c 
= str.charAt(i + 1);
                
if (c == '-' || c == '+')
                    i
++;
                
break;

            
default:
                
if (!Character.isDigit(c))
                    
return false;
                
break;
            }
        } 
while (++< len);
        
return true;
    }

    
public static boolean isInteger(String str) {
        
int len = str.length();
        
if (len == 0)
            
return false;
        
char c = str.charAt(0);
        
int i = c != '-' && c != '+' ? 0 : 1;
        
if (i >= len)
            
return false;
        
do
            
if (!Character.isDigit(str.charAt(i)))
                
return false;
        
while (++< len);
        
return true;
    }

    
public static boolean isUnsignedInteger(String str) {
        
int len = str.length();
        
if (len == 0)
            
return false;
        
for (int i = 0; i < len; i++)
            
if (!Character.isDigit(str.charAt(i)))
                
return false;

        
return true;
    }

    
public static String dequote(String str, char quote) {
        
if (str == null)
            
return null;
        
else
            
return dequote(str, 0, str.length(), quote);
    }

    
public static String dequote(String str, int begin, int end, char quote) {
        
if (begin == end)
            
return "";
        
int end_ = str.indexOf(quote, begin);
        
if (end_ < 0)
            
return str.substring(begin, end);
        StringBuffer sb 
= new StringBuffer(end - begin);
        
int begin_ = begin;
        
for (; end_ >= 0 && end_ < end; end_ = str.indexOf(quote,
                begin_ 
= end_ + 2)) {
            
if (end_ + 1 >= end || str.charAt(end_ + 1!= quote)
                
throw new IllegalArgumentException(
                        
"Internal quote not doubled in string '"
                                
+ str.substring(begin, end) + "'");
            sb.append(substring(str, begin_, end_)).append(quote);
        }

        
return sb.append(substring(str, begin_, end)).toString();
    }

    
public static String dequoteFull(String str, char quote) {
        
if (str == null)
            
return null;
        
else
            
return dequoteFull(str, 0, str.length(), quote);
    }

    
public static String dequoteFull(String str, int begin, int end, char quote) {
        
if (begin == end)
            
return "";
        
if (str.charAt(begin) != quote)
            
return str.substring(begin, end);
        
int _end = end - 1;
        
if (str.length() < 2 || str.charAt(_end) != quote)
            
throw new IllegalArgumentException(
                    
"Closing quote missing in string '"
                            
+ substring(str, begin, end) + "'");
        
else
            
return dequote(str, begin + 1, _end, quote);
    }

    
public static String replace(String str, String repl, String with) {
        
int lastindex = 0;
        
int pos = str.indexOf(repl);
        
if (pos < 0)
            
return str;
        
int len = repl.length();
        
int lendiff = with.length() - repl.length();
        StringBuffer out 
= new StringBuffer(lendiff > 0 ? str.length() + 10
                
* lendiff : str.length());
        
for (; pos >= 0; pos = str.indexOf(repl, lastindex = pos + len))
            out.append(substring(str, lastindex, pos)).append(with);

        
return out.append(substring(str, lastindex, str.length())).toString();
    }

    
public static String replace(String str, char repl, String with) {
        
int pos = str.indexOf(repl);
        
if (pos < 0)
            
return str;
        
int len = str.length();
        
int lendiff = with.length() - 1;
        StringBuffer out 
= new StringBuffer(lendiff > 0 ? str.length() + 10
                
* lendiff : str.length());
        
int lastindex = 0;
        
for (; pos >= 0; pos = str.indexOf(repl, lastindex = pos + 1))
            out.append(substring(str, lastindex, pos)).append(with);

        
return out.append(substring(str, lastindex, len)).toString();
    }

    
public static StringBuffer replace(StringBuffer out, String s, String repl,
            String with) {
        
int lastindex = 0;
        
int len = repl.length();
        
for (int index = s.indexOf(repl); index >= 0; index = s.indexOf(repl,
                lastindex 
= index + len))
            out.append(substring(s, lastindex, index)).append(with);

        
return out.append(substring(s, lastindex, len));
    }

    
public static String[] splitLongString(String str, char separator) {
        
int len;
        
if (str == null || (len = str.length()) == 0)
            
return EMPTY_STRING_ARRAY;
        
int oldPos = 0;
        ArrayList list 
= new ArrayList();
        
for (int pos = str.indexOf(separator); pos >= 0; pos = str.indexOf(
                separator, oldPos 
= pos + 1))
            list.add(substring(str, oldPos, pos));

        list.add(substring(str, oldPos, len));
        
return (String[]) list.toArray(EMPTY_STRING_ARRAY);
    }

    
public static String[] splitLongString(String str, char separator,
            
char quote) {
        
int len;
        
if (str == null || (len = str.length()) == 0)
            
return EMPTY_STRING_ARRAY;
        
int oldPos = 0;
        ArrayList list 
= new ArrayList();
        
for (int pos = 0; pos < len;) {
            
while (pos < len && str.charAt(pos) == quote) {
                pos 
= str.indexOf(quote, pos + 1+ 1;
                
if (pos == 0)
                    
throw new IllegalArgumentException(
                            
"Closing quote missing in string '" + str + "'");
            }
            
boolean quoted;
            
if (pos != oldPos) {
                quoted 
= true;
                
if (pos < len && str.charAt(pos) != separator)
                    
throw new IllegalArgumentException(
                            
"Separator must follow closing quote in string '"
                                    
+ str + "'");
            } 
else {
                quoted 
= false;
                pos 
= str.indexOf(separator, pos);
                
if (pos < 0)
                    pos 
= len;
            }
            list.add(quoted 
? ((Object) (dequote(str, oldPos + 1, pos - 1,
                    quote))) : ((Object) (substring(str, oldPos, pos))));
            oldPos 
= ++pos;
        }

        
return (String[]) list.toArray(EMPTY_STRING_ARRAY);
    }

    
public static String[] splitShortString(String str, char separator) {
        
int len;
        
if (str == null || (len = str.length()) == 0)
            
return EMPTY_STRING_ARRAY;
        
int lastTokenIndex = 0;
        
for (int pos = str.indexOf(separator); pos >= 0; pos = str.indexOf(
                separator, pos 
+ 1))
            lastTokenIndex
++;

        String list[] 
= new String[lastTokenIndex + 1];
        
int oldPos = 0;
        
int pos = str.indexOf(separator);
        
int i = 0;
        
for (; pos >= 0; pos = str.indexOf(separator, oldPos = pos + 1))
            list[i
++= substring(str, oldPos, pos);

        list[lastTokenIndex] 
= substring(str, oldPos, len);
        
return list;
    }

    
public static String[] splitShortString(String str, char separator,
            
char quote) {
        
int len;
        
if (str == null || (len = str.length()) == 0)
            
return EMPTY_STRING_ARRAY;
        
int tokenCount = 0;
        
int oldPos;
        
for (int pos = 0; pos < len; pos++) {
            tokenCount
++;
            oldPos 
= pos;
            
while (pos < len && str.charAt(pos) == quote) {
                pos 
= str.indexOf(quote, pos + 1+ 1;
                
if (pos == 0)
                    
throw new IllegalArgumentException(
                            
"Closing quote missing in string '" + str + "'");
            }
            
if (pos != oldPos) {
                
if (pos < len && str.charAt(pos) != separator)
                    
throw new IllegalArgumentException(
                            
"Separator must follow closing quote in strng '"
                                    
+ str + "'");
                
continue;
            }
            pos 
= str.indexOf(separator, pos);
            
if (pos < 0)
                
break;
        }

        
if (str.charAt(len - 1== separator)
            tokenCount
++;
        String list[] 
= new String[tokenCount];
        tokenCount
--;
        oldPos 
= 0;
        
int pos = 0;
        
for (int i = 0; i < tokenCount;) {
            
while (str.charAt(pos) == quote)
                pos 
= str.indexOf(quote, pos + 1+ 1;
            
boolean quoted;
            
if (pos != oldPos) {
                quoted 
= true;
                
if (str.charAt(pos) != separator)
                    
throw new IllegalArgumentException(
                            
"Separator must follow closing quote in strng '"
                                    
+ str + "'");
            } 
else {
                quoted 
= false;
                pos 
= str.indexOf(separator, pos);
            }
            list[i] 
= quoted ? dequote(str, oldPos + 1, pos - 1, quote)
                    : substring(str, oldPos, pos);
            i
++;
            oldPos 
= ++pos;
        }

        list[tokenCount] 
= dequoteFull(str, oldPos, len, quote);
        
return list;
    }

    
public static String substring(String str, int begin, int end) {
        
if (begin == end)
            
return "";
        
else
            
return str.substring(begin, end);
    }

    
public static String[] trim(String strings[]) {
        
if (strings == null)
            
return null;
        
int i = 0;
        
for (int len = strings.length; i < len; i++)
            strings[i] 
= strings[i].trim();

        
return strings;
    }

    
public static int minIndex(int a, int b) {
        
return a >= 0 ? b >= 0 ? a >= b ? b : a : a : b;
    }

    
public static final String EMPTY_STRING_ARRAY[] = new String[0];

}
posted on 2009-03-18 15:12 单飞 阅读(222) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: