/*
* Copyright (c) 2008-2011 Simon Ritchie.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/>.
*/
//package org.rimudb.editor.util;
public class StringUtil {
public static final String SC_LOWER = "lower";
public static final String SC_UPPER = "upper";
public static final String SC_SENTENCE = "sentence";
public static final String SC_TITLE = "title";
public static final String SC_PROPER = "proper";
/**
* Private Constructor - this is a static methods only class.
*/
private StringUtil() {
super();
}
/**
* Right justify a string using blanks to pad the left part of the
* string until it reaches maxLength
. If the length of
* the string is greater than maxLength
characters, return
* only the first, left maxLength
characters.
*
*/
public static String rightJustify( String s, int maxLength ) {
return rightJustify( s, maxLength, ' ' );
}
/**
* Right justify a string using the specified padding character to pad
* the left part of the string until it reaches maxLength
.
* If the length of the string is greater than maxLength
characters,
* return only the first, left maxLength
characters.
*
* @return The right-justified string.
*/
public static String rightJustify( String s, int maxLength, char fill ) {
if (s == null || maxLength == 0 ) {
return s;
}
// If the string has more than "maxLength" characters,
// return only the first "maxLength" characters of the string.
if ( s.trim().length() > maxLength ) {
return s.substring( 0, maxLength ).trim();
}
StringBuffer sb = new StringBuffer( s.trim() );
// Insert as many padding characters as needed to reach "maxLength".
while ( sb.length() < maxLength ) {
sb.insert( 0, fill );
}
return sb.toString();
}
/**
* Left justify a string, and fill to a given length with the blanks.
* If the length of the string is greater than "maxLength" characters, return only
* the first, left "maxLength" characters.
*/
public static String leftJustify(String s, int maxLength) {
return leftJustify(s, maxLength, ' ');
}
/**
* Left justify a string, and fill to a given length with the character fill
.
* If the length of the string is greater than "maxLength" characters, return only
* the first, left "maxLength" characters.
*/
public static String leftJustify(String s, int maxLength, char fill) {
if (s == null || maxLength == 0 ) {
return s;
}
// If the string has more than "maxLength" characters,
// return only the first "maxLength" characters of the string.
if ( s.trim().length() > maxLength ) {
return s.substring( 0, maxLength ).trim();
}
StringBuffer sb = new StringBuffer(s.trim());
// Append as many blanks as needed to reach "maxLength".
while ( sb.length() < maxLength ) {
sb.append(fill);
}
return sb.toString();
}
/**
* Replace the first occurrence of oldSubstring
in
* text
, if there is one, with newSubstring
.
*
* @param text - Replace a substring of this String
* @param oldSubstring - The substring of text
to be replaced
* @param newSubstring - The string to put into text
* @return A new String which is a copy of text
with the first
* occurrence of oldSubstring
in text
, if
* there is one, with newSubstring
.
* Returns null if text
is null.
* Returns text
if either substring is null or
* oldSubstring
is empty
*/
public static String replace(String text, String oldSubstring, String newSubstring) {
String result = text;
if ((text != null) && (text.length() > 0)
&& (oldSubstring != null) && (oldSubstring.length() > 0)
&& (newSubstring != null)) {
int pos = text.indexOf(oldSubstring);
if (pos > -1) {
result = text.substring(0, pos) + newSubstring + text.substring(pos + oldSubstring.length());
}
}
return result;
}
/**
* Replaces all occurrences of oldSubstring
in
* text
, if there are any, with newSubstring
.
*
* @param text - Replace substrings of this String
* @param oldSubstring - The substring of text
to be replaced
* @param newSubstring - The string to put into text
* @return A new String which is a copy of text
with all
* occurrences of oldSubstring
in string
,
* if there are any, with newSubstring
.
* Returns null if text
is null.
* Returns text
if either substring is null or
* oldSubstring
is empty
*/
public static String replaceAll(String text, String oldSubstring, String newSubstring) {
String result = text;
if ( (result != null)
&& (result.length() > 0)
&& (result.indexOf(oldSubstring)>-1)
&& (oldSubstring.length() > 0)
&& (!oldSubstring.equals(newSubstring))) {
while (result.indexOf(oldSubstring) > -1) {
result = replace(result, oldSubstring, newSubstring);
}
}
return result;
}
/**
* Convert the String s to proper case.
*
* @param s String
* @return String
*/
public static String toProperCase(String s) {
StringBuilder sb = new StringBuilder();
for (String f: s.split(" ")) {
if (sb.length()>0) {
sb.append(" ");
}
sb.append(f.substring(0,1).toUpperCase()).append(f.substring(1,f.length()).toLowerCase());
}
return sb.toString();
}
}