StringHelper.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.util |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.util;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;
/**
* Provides a set of static methods that help with string parsing and modifying.
*/
public class StringHelper
{
//--- Constant(s) ---
public static final String ALPHABET_LOWER = "abcdefghijklmnopqrstuvwxyz";
public static final String ALPHABET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String NUMBERS_INT = "0123456789";
public static final String NUMBERS_DECIMAL = NUMBERS_INT + ".";
public static final String MONEY_USD = "$" + NUMBERS_DECIMAL;
public static final String ALPHA_NUM
= ALPHABET_LOWER + ALPHABET_UPPER + NUMBERS_INT;
public static final String EMAIL = ALPHA_NUM + ".@_-";
public static final String HOST = ALPHA_NUM + ".";
public static final String REGULAR_STRING
= EMAIL + "[]ß$%/\\(){}fl?‰ˆ¸+-*,;.<>|_^∞~#";
public static final String ANYTHING = null;
//--- Data field(s) ---
//--- Constructor(s) ---
//--- Method(s) ---
/**
* Replaces all occurences of <code>oldChars</code> in <code>s</code> by
* <code>newChars</code>.
*
* @return the modified instance of <code>s</code>
*/
public static String replaceAll(String s, String oldChars, String newChars)
{
StringBuffer sb = new StringBuffer();
int i = 0;
while (true) {
int j = s.indexOf(oldChars, i);
if (j != -1) {
sb.append(s.substring(i, j));
sb.append(newChars);
i = j + oldChars.length();
}
else {
sb.append(s.substring(i));
break;
}
}
return sb.toString();
}
/**
* Returns a random lower case letter-only string with <code>length</code>
* characters.
*/
public static String randomString(int length)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
sb.append((char)Math.round(Math.random() * 25 + 97));
}
return sb.toString();
}
/**
* Returns a new <code>String</code> that has the first letter of value
* set to upper case.
*/
public static String toFirstUpper(String value)
{
if (value.length() > 1) {
return Character.toUpperCase(value.charAt(0)) + value.substring(1);
}
else {
return value.toUpperCase();
}
}
/**
* Removes all characters from <code>s</code> that are not letters.
* Returns a new String which can be for instance used as search text.
*
* @return a stripped instance of s
*/
public static String stripExtra(String s)
{
StringBuffer sb = new StringBuffer();
boolean newWord = false;
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (Character.isLetter(chars[i])) {
if (newWord) {
sb.append(" ");
newWord = false;
}
sb.append(chars[i]);
}
else {
newWord = true;
}
}
return sb.toString();
}
/**
* Returns the index of the first digit in <code>s</code>.
*
* @return -1, if <code>s</code> does not contain digits; the index of
* the first digit, otherwise
* @see java.lang.String#indexOf(String)
*/
public static int indexOfDigit(String s)
{
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
return i;
}
}
return -1;
}
/**
* Splits <code>text</code> at the first occurence of <code>separator</code>
* and returns the left part, excluding the separator.
*
* @param text the haystack
* @param separator the needle
* @return the empty string, if no occurence can be found
*/
public static String firstToken(String text, String separator)
{
if (separator == null) {
throw new IllegalArgumentException("separator must not be null");
}
if (separator.length() == 0) {
throw new IllegalArgumentException("separator must not be empty");
}
int i = text.indexOf(separator);
return (i == -1) ? text : text.substring(0, i);
}
/**
* Splits <code>text</code> at the last occurence of <code>separator</code>
* and returns the right part, excluding the separator.
*
* @param text the haystack
* @param separator the needle
* @return the empty string, if no occurence can be found
*/
public static String lastToken(String text, String separator)
{
if (separator == null) {
throw new IllegalArgumentException("separator must not be null");
}
if (separator.length() == 0) {
throw new IllegalArgumentException("separator must not be empty");
}
int i = text.lastIndexOf(separator);
if (i < 0 || i == text.length() - 1) {
return "";
}
else {
return text.substring(i + separator.length(), text.length());
}
}
/**
* Splits <code>text</code> at the last occurence of <code>separator</code>
* and returns the left part, excluding the separator.
*
* @param text the haystack
* @param separator the needle
* @return the empty string, if no occurence can be found
*/
public static String lastPrefix(String text, String separator)
{
if (separator == null) {
throw new IllegalArgumentException("separator must not be null");
}
if (separator.length() == 0) {
throw new IllegalArgumentException("separator must not be empty");
}
int i = text.lastIndexOf(separator);
return (i == -1) ? text : text.substring(0, i);
}
public static String[] toArray(String value, String separators)
{
StringTokenizer st = new StringTokenizer(value, separators);
String[] array = new String[st.countTokens()];
for (int i = 0; i < array.length; i++) {
array[i] = st.nextToken();
}
return array;
}
public static int[] toIntArray(String value, String separators)
{
StringTokenizer st = new StringTokenizer(value, separators);
int[] array = new int[st.countTokens()];
for (int i = 0; i < array.length; i++) {
try {
array[i] = Integer.parseInt(st.nextToken());
}
catch (NumberFormatException e) {
}
}
return array;
}
public static LinkedList toList(String value, String separators)
{
StringTokenizer st = new StringTokenizer(value, separators);
LinkedList list = new LinkedList();
while (st.hasMoreTokens()) {
list.add(st.nextToken());
}
return list;
}
/**
* Tries to autodect encoding.
*/
public static String toString(byte[] data)
{
try {
return new String(data, "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace(System.err);
}
try {
return new String(data, "ISO-8859-1");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace(System.err);
}
return new String(data);
}
public static String toString(int[] array, String separator)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; i++) {
sb.append(array[i]);
sb.append(separator);
}
return sb.toString();
}
public static String toString(String[] array, String separator)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; i++) {
sb.append(array[i]);
sb.append(separator);
}
return sb.toString();
}
public static String toString(Collection c, String separator)
{
StringBuffer sb = new StringBuffer();
for (Iterator i = c.iterator(); i.hasNext();) {
sb.append(i.next().toString());
sb.append(separator);
}
return sb.toString();
}
}
The table below shows all metrics for StringHelper.java.




