This commit is contained in:
parent
fe7cf55de9
commit
51e9da7c9b
8 changed files with 118 additions and 156 deletions
|
|
@ -7,7 +7,7 @@ package zutil;
|
|||
*/
|
||||
public class StringUtil {
|
||||
public static final String[] sizes = new String[]{"YB", "ZB", "EB", "PB", "TB", "GB", "MB", "kB", "B"};
|
||||
|
||||
|
||||
/**
|
||||
* Present a size (in bytes) as a human-readable value
|
||||
*
|
||||
|
|
@ -26,5 +26,52 @@ public class StringUtil {
|
|||
return value+" "+sizes[total];
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the given char and whitespace at the beginning and the end
|
||||
*
|
||||
* @param str is the string to trim
|
||||
* @param trim is the char to trim
|
||||
* @return a trimmed String
|
||||
*/
|
||||
public static String trim(String str, char trim){
|
||||
if( str == null || str.isEmpty() )
|
||||
return str;
|
||||
int start=0, stop=str.length();
|
||||
// The beginning
|
||||
for(int i=0; i<str.length() ;i++){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
start = i+1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// The end
|
||||
for(int i=str.length()-1; i>start ;i--){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
stop = i;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if( start >= str.length() )
|
||||
return "";
|
||||
//System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop);
|
||||
return str.substring(start, stop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the whitespace and quotes if the string starts and ends with one
|
||||
*
|
||||
* @param str is the string to trim
|
||||
* @return
|
||||
*/
|
||||
public static String trimQuotes(String str){
|
||||
if( str == null )
|
||||
return null;
|
||||
str = str.trim();
|
||||
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
||||
str = str.substring(1, str.length()-1);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue