2011-01-02 06:50:31 +01:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* 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 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.util;
|
|
|
|
|
2011-02-01 11:03:18 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2011-01-02 06:50:31 +01:00
|
|
|
/**
|
|
|
|
* String utilities.
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
|
|
|
public class StringUtil {
|
|
|
|
/**
|
|
|
|
* Trim a string if it is longer than a certain length.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param len
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String trimLength(String str, int len) {
|
|
|
|
if (str.length() > len) {
|
|
|
|
return str.substring(0, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join an array of strings into a string.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param delimiter
|
|
|
|
* @param initialIndex
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String joinString(String[] str, String delimiter,
|
|
|
|
int initialIndex) {
|
|
|
|
if (str.length == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
StringBuilder buffer = new StringBuilder(str[initialIndex]);
|
|
|
|
for (int i = initialIndex + 1; i < str.length; i++) {
|
|
|
|
buffer.append(delimiter).append(str[i]);
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
2011-01-29 21:46:22 +01:00
|
|
|
/**
|
|
|
|
* Join an array of strings into a string.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param delimiter
|
|
|
|
* @param initialIndex
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String joinQuotedString(String[] str, String delimiter,
|
|
|
|
int initialIndex, String quote) {
|
|
|
|
if (str.length == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
StringBuilder buffer = new StringBuilder();
|
|
|
|
buffer.append(quote);
|
|
|
|
buffer.append(str[initialIndex]);
|
|
|
|
buffer.append(quote);
|
|
|
|
for (int i = initialIndex + 1; i < str.length; i++) {
|
|
|
|
buffer.append(delimiter).append(quote).append(str[i]).append(quote);
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
2011-01-19 11:03:41 +01:00
|
|
|
/**
|
|
|
|
* Join an array of strings into a string.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param delimiter
|
|
|
|
* @param initialIndex
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String joinString(String[] str, String delimiter) {
|
|
|
|
return joinString(str, delimiter, 0);
|
|
|
|
}
|
|
|
|
|
2011-01-02 06:50:31 +01:00
|
|
|
/**
|
|
|
|
* Join an array of strings into a string.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param delimiter
|
|
|
|
* @param initialIndex
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String joinString(Object[] str, String delimiter,
|
|
|
|
int initialIndex) {
|
|
|
|
if (str.length == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
StringBuilder buffer = new StringBuilder(str[initialIndex].toString());
|
|
|
|
for (int i = initialIndex + 1; i < str.length; i++) {
|
|
|
|
buffer.append(delimiter).append(str[i].toString());
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join an array of strings into a string.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param delimiter
|
|
|
|
* @param initialIndex
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String joinString(int[] str, String delimiter,
|
|
|
|
int initialIndex) {
|
|
|
|
if (str.length == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
StringBuilder buffer = new StringBuilder(Integer.toString(str[initialIndex]));
|
|
|
|
for (int i = initialIndex + 1; i < str.length; i++) {
|
|
|
|
buffer.append(delimiter).append(Integer.toString(str[i]));
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
2011-02-01 11:03:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Join an list of strings into a string.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param delimiter
|
|
|
|
* @param initialIndex
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String joinString(List<String> str, String delimiter,
|
|
|
|
int initialIndex) {
|
|
|
|
if (str.size() == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
StringBuilder buffer = new StringBuilder(str.get(initialIndex).toString());
|
|
|
|
for (int i = initialIndex + 1; i < str.size(); i++) {
|
|
|
|
buffer.append(delimiter).append(str.get(i).toString());
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
2011-01-02 06:50:31 +01:00
|
|
|
}
|