com.venky.core.string.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common Show documentation
Show all versions of common Show documentation
Commonly used programming tasks in java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.venky.core.string;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
import java.util.Stack;
/**
*
* @author venky
*/
public class StringUtil {
public static boolean equals(String s1, String s2){
if (s1 == null){
return s2 == null;
}else {
return valueOf(s1).equals(valueOf(s2));
}
}
public static String valueOf(Object o){
return String.valueOf(o == null? "" : o).trim();
}
public static String camelize(String underscorizedString) {
return camelize(underscorizedString,true);
}
public static String camelize(String underscorizedString,boolean capitalizeFirstChar) {
return Inflector.camelize(underscorizedString,capitalizeFirstChar);
}
public static String underscorize(String camel){
return Inflector.underscore(camel);
}
public static String pluralize(String singular){
return Inflector.pluralize(singular);
}
public static String singularize(String plural){
return Inflector.singularize(plural);
}
public static String read(InputStream in){
return read(in,true);
}
public static String read(InputStream in,boolean close){
return read(new BufferedReader(new InputStreamReader(in)), close);
}
public static String read(Reader in){
return read(in,true);
}
public static String read(Reader reader, boolean close){
BufferedReader r = new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
char[] buffer = new char[1024];
try {
int numCharsRead = 0;
while ((numCharsRead = r.read(buffer)) > 0){
builder.append(buffer,0,numCharsRead);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (close){
r.close();
}
}catch(IOException ex) {
throw new RuntimeException(ex);
}
}
return builder.toString();
}
public static byte[] readBytes(InputStream in){
return readBytes(in,true);
}
public static byte[] readBytes(InputStream in, boolean close){
if (in == null){
return new byte[]{};
}
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
try {
int numbytesRead = 0;
while ((numbytesRead = in.read(buffer)) > 0){
out.write(buffer,0,numbytesRead);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (close){
in.close();
}
}catch(IOException ex) {
throw new RuntimeException(ex);
}
}
return out.toByteArray();
}
public static String toWords(int number){
return new NumberToWordsConverter(number).toString();
}
public static String format(String template, Properties properties){
StringBuilder formattedString = new StringBuilder();
char[] chars = template.toCharArray();
Stack variables = new Stack();
for (int i = 0 ; i